4

How to check that iteration will not be over None? I wrote the following code:

async def foo(categories: list=None):
    for category in categories or []:

Is it a good way or I can avoid somehow checking or [] in every iteration?

petrush
  • 885
  • 7
  • 20

1 Answers1

2

Let's test this out.

def foo():
    print('In foo')
    return range(5)

Now, if I call foo in the head of a for loop (for _ in foo()), it should print In foo as many times as it is called/evaluated.

So,

for _ in foo():
    pass

In foo

And is called just once. Meaning it was evaluated only once.

cs95
  • 330,695
  • 80
  • 606
  • 657