Hacker News new | ask | show | jobs
by peterthehacker 1683 days ago
> I've tried Python, hated the inconsistency of it

Can you elaborate on this? I’ve written in both Python and Ruby, but I’m not sure what you mean by this critique of python. I wouldn’t characterize either language as “inconsistent.”

2 comments

In Python some standard operations are global functions and some are methods. Which are which isn't exactly clear. It's also kind of irrational and random but I hate seeing __init__ everywhere.

For example, to find the length of an array in Python you use the function len(). In Ruby you call the method .length. As the article is about, loops in Python are for blah in blah. In Ruby they're blah.each and for loops are just sugar (not sure why they're there but you can avoid them). In Ruby pretty much everything is an object and you just call methods. Very few keywords, global functions, etc...

Also, as the other poster said, if you call a[0] on an empty array, in Ruby you get nil, in Python it throws an error. Not sure which is better or considered more 'proper', but Ruby's behaviour is what I'd personally expect in a dynamic language.

> In Python some standard operations are global functions and some are methods.

Typically, all of the former are also the latter because the global function just calls the corresponding method. E.g., len(x) works by calling x.__len__().

> As the article is about, loops in Python are for blah in blah. In Ruby they're

...for blah in blah, if you choose to use them at all, which has the same semantics as the python.

It relies on method calls to #each under the hood, and usually in Ruby you won't bother with for/in, but it is there still...

a[0]. a is an array. If the array is empty, what's the result, in both languages?