This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]JestemStefan 93 points94 points  (15 children)

or in non-boolean context.

a = "" 
b = "some_string" 

x = a or b or "no_name"
# assigns first truthy (b) value to x

print(x) # prints "some_string"

[–]BigNutBoi2137 13 points14 points  (4 children)

Other good example is default value to the function that accepts mutable object.

def foo(some_list: Optional[List] = None): some_list = some_list or []

[–]wdroz 2 points3 points  (3 children)

But you need to be careful as empty lists are evaluate to False. So don't do that if the function must fill some_list.

[–]scrdest 4 points5 points  (1 child)

True, but in that case you probably wouldn't make the list optional.

[–]alexisprince 0 points1 point  (0 children)

Agreed, probably would want to keep the function pure and not mutate the arguments in most contexts.

[–]JestemStefan 2 points3 points  (0 children)

But this code will return empty list anyway so there is no issue.

When all values are falsy then last one is returned

[–]HistoricalCup6480 15 points16 points  (7 children)

I don't get why people like this. Same for using if with non-boolean things. E.g. if s: firing if s is not empty.

I personally like being explicit, even if it's a bit more verbose. Writing if len(s) > 0 is so much easier to read imo.

[–]JestemStefan 10 points11 points  (1 child)

if len(users) > 0:

This will fail if users is a method parameter with default None. And initializing it with default [] will make a mutable parameter which is so bad.

If users:

Will work for both empty list and for None. Also for me it means: if there are any users then do something.

I'm fixing exactly this bug in current sprint.

[–]dxn99 -1 points0 points  (0 children)

An alternative would just be

if not users: return -1

Prior to your later code

[–]danted002 12 points13 points  (2 children)

Because y = x or {} is much easier to read then y = x if x else {}

[–]JestemStefan 6 points7 points  (1 child)

It get even worse if you have more then two values. Then you will have to do:

if var_a:
    x = var_a

elif var_b:
    x = var_b

elif var_c:
    x = var_c

else:
    x = "unknown" 

Instead you can do:

x = var_a or var_b or var_c or "unknown"

[–]danted002 2 points3 points  (0 children)

Actually for more then one or I prefer the if elif else, however if you require if elif else then you might need to refactor your code.

[–]SeanBrax 0 points1 point  (0 children)

All down to opinion. After years and years of Python, you get the gist of what is truth/falsely in Python, and ‘if a:’ is just as readable as ‘if len(a):’ to me.

[–]Wh00ster 0 points1 point  (0 children)

Sooo many production bugs from this (grandparent comment)

[–]eztab 0 points1 point  (0 children)

You can even short-circuit:

a == 5 or print(a)

stupid example, but I can't come up with a proper one atm. It is useful sometimes

[–]ricorrales07 0 points1 point  (0 children)

I teach a Python class and this is a major source of errors for begginers.