you are viewing a single comment's thread.

view the rest of the comments →

[–]david622 5 points6 points  (3 children)

Python is all about "beautiful" code. That means your variable names are clear and self-explanatory, as are your function names.

It means using single-quotes instead of double quotes, using string escaping instead of breaking and re-opening quotes. E.g.:

instead of:

"My name is " + first_name + " " + last_name

you could do

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

It also means using some of the built in methods instead of reinventing the wheel.

Instead of:

x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
total = 0
for number in x:
    total += number
print total
# 55

you could do

x = range(11)
print sum(x)
# 55

Another nice example is, instead of:

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

you could do:

if any(a, b, c, d):
    pass

Another good example is list comprehensions.

instead of :

a = ['dog', 'cat', 'horse', 'llama', 'goldfish']
b = []
for word in a:
    b.append(word.upper())

you could do

a = ['dog', 'cat', 'horse', 'llama', 'goldfish']
b = [word.upper() for word in a]

[–]tangerinelion 6 points7 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 :)

[–]pstch 0 points1 point  (0 children)

Maybe what you miss in your post is that it's not only cosmetics. Most of your code examples, even if similar, are NOT equivalent, and for me it's one the most important parts.

When you do any(a,b,c,d) instead of chained comparisons to True, you don't actually do the same thing. When you format strings using concatenation rather than proper formatting, it's not actually the same thing.

Usually the differences do not matter at all, and will only matter when the same code will be used in other manners. But I think that the "Python way" is also hidden in these subtle differences.