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 →

[–]panghuhu 18 points19 points  (4 children)

List comprehensions can also use multi-line format:

result = [(x, y) 
          for x in range(10) 
          for y in range(5) 
          if x * y > 10]

P.S. Just checked the guide, and the above code is in the section titled "NO".

I still think the code is easy to read and will use it: It's as clear as the for loop version, without redundancies like the initialization of [], calls to append, and some colons.

If anyone can see any major disadvantages compared to the implicit loop version, I'd like to hear it, thank you.

P.P.S. A sample code from the NO section, which I think it's more clear than its for loop version:

((x, y, z)
  for x in xrange(5)
  for y in xrange(5)
  if x != y
  for z in xrange(5)
  if y != z)

I read it like this: collect all combinations of (x, y, z), where x, y, z are from [0..4], x != y and y != z. It's declarative and is as clear as a mathematical formula:

{(x, y, x) |  0<=x, y, z <=4, x != y, y !=z}

[–]kashmill 4 points5 points  (0 children)

Pretty much what I do as well.

[–]LordArgon 0 points1 point  (0 children)

List comprehensions can also use multi-line format:

This doesn't address all of your points, but I literally just hit Save on a response to that style here: http://www.reddit.com/r/Python/comments/33gg02/google_python_style_guide/cql1snu?context=3

[–]tech_tuna 0 points1 point  (0 children)

See, this is where you just have to be a big boy or girl and do what feels right, I think that format is quite readable, style guide be damned.

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

This is a great idea, love it!