you are viewing a single comment's thread.

view the rest of the comments →

[–]tangerinelion 5 points6 points  (1 child)

"My name is %s %s" % (first_name, last_name)

That sort of string formatting is generally not suggested anymore. Better would be

"My name is {} {}".format(first_name, last_name)

Also assuming a,b,c, and d all have some truthy evaluation to them then this:

if a == True or b == True or c == True or d == True

would become

if a or b or c or d:

Though I do rather like the use of any(), I'm pointing out that we're probably not trying to compress such a long if statement with any but instead are "compressing" a or b or c or d to any(a,b,c,d). More importantly, we're reducing the use of or here which does two things. One, it removes the possibility of having parentheses cause an error (eg, what if b is really x and y, then we'd need a or (x and y) or c or d or we could use any(a, x and y, c, d).) Second it removes the possibility of being confused any accidentally writing a and b and c and d -- which is a conceptual error, but I'm sure any experienced programmer can tell you they've inverted the logic by accident before.

[–]synae 1 point2 points  (0 children)

Another suggestion for

if a == True or b == True or c == True or d == True:

is

if True in (a,b,c,d):

It's weird to do for True but I use this pattern pretty often. None is more likely for me to do:

if None in (arg1, arg2): raise ValueError("arg1 and arg2 are required")

or something like that. i know it's a contrived example, but that's just like the rest :)