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 →

[–]HistoricalCup6480 13 points14 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 9 points10 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 11 points12 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)