Advertisement

ruby language - what do you think about?

Started by July 14, 2004 02:23 AM
11 comments, last by GnuVince 20 years, 4 months ago
Quote: Original post by Kylotan
len isn't a method because it operates on arbitrary sequence objects. It defers implementation to that object, which often implements it as a __len__ method, but also applies to iterators too I believe.


In Ruby, you have the pretty much the following object hierarchy:
Object|__ Enumerable    |__ Array    |__ String    |__ Hash


And Enumerable implements length (and aliases it to size too), so you can do [1,2,3].len or "hello".len without any problems.
Python doesn't tend to have inheritance hierarchies like that, so I'm not sure how relevant that is.

I used to know the reasonably-good reason why len isn't a member, but I can't find it. Maybe it's to do with encouraging the functional paradigm. Maybe it's because you're expected to be able to call it on objects that have no concept of length and get a reasonable exception back, rather than a 'no such method' exception.
Advertisement
Quote: Original post by Kylotan
Python doesn't tend to have inheritance hierarchies like that, so I'm not sure how relevant that is.

I used to know the reasonably-good reason why len isn't a member, but I can't find it. Maybe it's to do with encouraging the functional paradigm. Maybe it's because you're expected to be able to call it on objects that have no concept of length and get a reasonable exception back, rather than a 'no such method' exception.


Well, seeing as how Python now discourages the usage of filter() and appl() in favor of list comprehension, I don't think that this is the reason. So the functional paradigm is out.

As for the reasonable exception, I don't think that's it. Basically, I'd say that since Python did not originally start out object-oriented (Ruby did), they made len() a generic function. I'd say this is the reason. But it's still weird that they changed strings from:
 import stringstring.replace("hello", "h", "j") # hello -> jello


to :
"hello".replace("h", "j")


I mean, if you're going to change that, might as well be consistant and change the whole thing. Anyway, that's what of my irks with Python, but on the whole, I really like the language.

This topic is closed to new replies.

Advertisement