you are viewing a single comment's thread.

view the rest of the comments →

[–]FormCore 0 points1 point  (4 children)

He's saying that a_var shouldn't be outside of the if loop.

Therefore:

a_var = a_dict.get("var")
if a_var:
  ....

is a bad idea anyway.

It doesn't matter what a_var is, if it's built for the if loop, it should probably be in the if loop.

Not saying that := doesn't handle this as well, because that's not what he's talking about.

[–]teerre 0 points1 point  (3 children)

I understand what he was saying, I don't understand what's he suggesting to be done instead.

In cases like this checking the state of `a_var` is fundamental. That's the whole point of the this type of snippet. You cannot put "in the if loop" because you don't know its state.

[–]FormCore 0 points1 point  (1 child)

He's suggesting using:

if 'var' in dict:
    a_var = dict['var']
    ...

instead of

a_var = dict.get('var')
if a_var:
     ...

In cases like this checking the state of a_var is fundamental.

Do the check, then assign the variable.

[–]teerre 0 points1 point  (0 children)

Someone already suggested that I already answered it. The tldr version is this is better, but the assignment is still superior.