Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I find the "there's only one way to do it" philosophy of Python to be really important. That's the main thing that kept Ruby off my radar.


I feel it diverges a little bit, actually. In many cases it provides the "C style" way of doing it and the "functional style" way of doing it. Thankfully, in many cases one of them is much faster than the other, but it's not obvious to the novice and they will probably use the "C style" for years in ignorance of the alternative.

Example:

  mylist = []
  for char in "foobar":
      if char == "f":
          mylist.append(char)
vs.

  mylist = [ char for char in "foobar" if char == "f" ]
The latter is supposedly much faster, and as you can see, much more succinct, but I hadn't learned about it until about a year ago. I have been programming in Python since about 2002/2003.


It's pretty hard to believe you've been using Python seriously since 2002/2003 and had never heard of list comprehensions. They are a pretty big staple of most Python programmers.


Then you can call me out for being a noob, if you wish. :)


That's a good point. I'd argue the first way is the wrong way to do it. I guess once you get to multiline statements the philosophy breaks down. I'm just happy there's no string.strip() and string.trim() or something crazy like that which I'm guessing Perl has :-)?


This particular example could be written as follows:

  >>> char = 'f'
  >>> char * "foobaf".count(char)
  'ff'
Or

  >>> [char] * "foobaf".count(char)
  ['f', 'f']




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

Search: