Hacker News new | past | comments | ask | show | jobs | submit login

"using map allows you to break at commas instead of embedding backslashes in your code"

I usually break list comprehensions before the 'for', and then again before the 'if' if they still need it. No backslashes. Implicit line continuation works as well within list comprehensions as it does in function calls.

"if [number for number in numbers if number < 10]:"

I would've used:

  if any(number < 10 for number in numbers):
Or alternatively

  if any(itertools.imap(lambda x: x < 10, numbers)):
'any' short circuits its evaluation if it finds a true item , and the generator expression doesn't need to be evaluated fully. (I think the first is more readable.)

I also didn't find all that much that was new and interesting in the article, but that's because I went hunting for articles like this when I first started getting into Python seriously, and have been programming full-time in it for 8 months now.




I usually break list comprehensions before the 'for', and then again before the 'if' if they still need it. No backslashes. Implicit line continuation works as well within list comprehensions as it does in function calls.

Wow, I learned more from this comment than I did from the entire wiki page. I wonder why I never realized this before... I tend to break my list comprehensions the same way, and for some reason I thought the backslashes were necessary.

  if any(number < 10 for number in numbers):
Very nice. I need to start using some of these builtins a little more. In particular, I'm pretty sure that I've used

  for foo, index in izip(list, count())
before instead of simply using enumerate. any is one that I'll definitely have to keep in mind.


  if any(number < 10 for number in numbers):
Okay this is has to be the best way to do this, much better than the example in the article.


except it is not working?

>>> numbers = [1,10,100,1000,10000,100000,1000000] >>> if any(number < 10 for number in numbers):print number

Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> if any(number < 10 for number in numbers):print number NameError: name 'number' is not defined >>> if any(number < 10 for number in numbers): print number

Traceback (most recent call last): File "<pyshell#7>", line 2, in <module> print number NameError: name 'number' is not defined >>>


It is working; the variable "number" is local to the generator expression and can't be used in the print statement. It works if you print something else instead (e.g. "Success!" in the article).

If you just want to print the numbers less than 10, it's easier to do:

  print [number for number in numbers if number < 10]
or

  print '\n'.join(str(number) for number in numbers if number < 10)
if you want the same formatting.


This is where haskell makes me jealous

    if any (< 10) numbers
is so much prettier.

I really want a dynamic language with haskell's beautiful function composition.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: