3 ms·
All the popular scripting languages "do" the same things. But I prefer Ruby to Python (while still liking Python) because it feels more programmer-friendly.
by asher 16y ago
All the popular scripting languages "do" the same things. But I prefer Ruby to Python (while still liking Python) because it feels more programmer-friendly.
Ruby: a = %w( apple pear orange )
Python: a = [ "apple", "pear", "orange" ]
Ruby: a=%q/O'Malley says "Hello"/
Python: a = "O'Malley says \"Hello\""
Python lets you use "'" or '"' as the delimiter, which solves 80% of these cases - but why not generalize the concept, as Perl and Ruby did?
In Ruby, the last expression in a function is the return value; in Python you must use the return keyword, even though there's no ambiguity.
- dlsspy 16y agoa = '''O'Malley says "Hello"'''
- kragen 16y agoMy usual approach to the first one in Python is: a = "apple pear orange".split()
- Goladus 16y agoWhile it's true that "last expression in a function" is not technically ambiguous, because of python's statement/expression distinction leaving off the return can lead to unclear code. For example, if there's a function composed entirely of print statements, the last expression evaluated will be the expression in the last print statement executed. But, that means you have to know that 'print' is a statement and not an expression, and that the right-hand side of the print statement is in fact an expression. In ruby, actions like prints and assignments are side-effects and always evaluated as expressions. So if puts is the last expression in a function you'll get the return value of puts (which is usually nil I believe). That's pretty simple.