News for hackers. Stuff that matters.
Re:What's better about Ruby than Python? - comp.lang.python by Hemospectrum on Thursday August 26, 2010 @10:46PM (#1637870)
Then why bother with OOP to begin with?
Re:What's better about Ruby than Python? - comp.lang.python by jsharpe on Thursday August 26, 2010 @11:05PM (#1637899)
Having a global len() function is very, very linked to polymorphism. You can call it on anything that has a __len__() function defined (i.e., any class that implements something like a "HasLength" interface in Java terms).
Re:What's better about Ruby than Python? - comp.lang.python by silversmith on Friday August 27, 2010 @04:55AM (#1638318)
If you have __len__() implemented on the object, why not call it directly? If you are dealing with object X, that is of classes A,B,C or D (that all implement __len__), I don't see the fundamental difference between len(X) and X.__len__().
Re:What's better about Ruby than Python? - comp.lang.python by etal on Friday August 27, 2010 @10:15AM (#1638857)
The "magic methods" wrapped in in double-underscores are special because other parts of the language's syntax rely on them always meaning the same thing. (Some room for programmer misbehavior here.)

Example: If __len__ is defined on an object x, and __nonzero__ (Py2) or __bool__ (Py3) is not defined, then "not x" will test if x.__len__() == 0.

Similarly, __contains__ interacts with "foo in x", and __eq__ and __hash__ are expected to play well together for hashable objects (e.g. unique members of a set).

The double underscores are a useful code smell that indicates you're changing how an object interacts with Python's syntax.

Re:What's better about Ruby than Python? - comp.lang.python by avibryant on Saturday August 28, 2010 @01:24AM (#1641256)
I've been puzzled by len() and company for a long time and this is the most complete and sensible explanation I've heard. Thank you.

I still think it's the wrong design, mind you, but I no longer think it's a crazy one.

Re:What's better about Ruby than Python? - comp.lang.python by j_baker on Friday August 27, 2010 @03:24PM (#1639899)
Tomato, tomahto.

Why use x.__len__() rather than len(x)?

Re:What's better about Ruby than Python? - comp.lang.python by megaman821 on Friday August 27, 2010 @10:47AM (#1638953)
There really is no difference except that len() will raise an error if no __len__ method is found on the object.