r/programming • • Aug 30 '08

Tour De Babel -- Rant about programming languages (at Amazon)

http://steve.yegge.googlepages.com/tour-de-babel
15 Upvotes

122 comments sorted by

View all comments

Show parent comment

7

u/[deleted] Aug 31 '08

All three of those functions are built-ins because they operate on any object that looks like a sequence, including lists, tuples, generators, iterators, strings, and any user-defined object that can be iterated over. If I want to write a list-like object without inheriting from list, should I have to write stub map/reduce/len methods just in case someone wants to use them? Wouldn't it be smarter to implement these very generic functions as something that will work on any list-like object?

9

u/akdas Aug 31 '08

If I want to write a list-like object without inheriting from list, should I have to write stub map/reduce/len methods just in case someone wants to use them?

In Ruby, you don't have to inherit from Array to get a bunch of methods that operate on list-like objects for free by simply includeing the Enumerable module.

Wouldn't it be smarter to implement these very generic functions as something that will work on any list-like object?

Like Ruby's Enumerable.

I like Python, and I understand it has a different philosophy, but examples like this are why I prefer Ruby (once again, it's purely a personal preference).

Now if only Ruby's built-in file manipulation libraries were better.

5

u/ubernostrum Aug 31 '08 edited Aug 31 '08

Well.

From the Python perspective I like the fact that len() is a built-in that delegates to a double-underscore method. I like the fact that a lot of other functionality works that way, too, because it means I don't have to treat a bunch of otherwise-usable public identifiers as reserved because someone might interpret their presence as meaning that I'm implementing some API.

I also like the fact that Python deliberately sets off the names of the "special" methods so you know something different is going on.

And there's a certain sparseness that results from the Python approach which appeals to me; for example, Ruby's Enumerable includes at least four methods which simply consist of "return all the items in the collection which match/don't match some condition". As a Python programmer, I instinctively ask:

  • Why should I need to tie up so many names in my class just to do one thing, when filter() or a list comprehension would do just as well?
  • Why should I have to remember which methods take which arguments (some take regular expressions, some take blocks...) instead of having a unified interface that can behave intelligently?

Similarly, there are at least three methods in Enumerable which basically boil down to "execute this code once for each item"; again, why tie up so many names when I could just use map() or a comprehension? Why have multiple methods instead of a unified interface?

1

u/shifty3 Aug 31 '08

What he said.

I'm not that much of a Python programmer, but shouldn't the same solution be possible in Python? As far as I know, it also supports mixins.

1

u/Leonidas_from_XIV Aug 31 '08

It supports mixins via Multiple Inheritance which has become somehow rare in todays OOP languages. I'm not really sure why exactly it has been considered evil.

2

u/twotime Aug 31 '08

I agree with you on map/reduce (they utilize a low-level property of objects (iterability)).

But len() is a somewhat different beast: is not it defined as

def len(x): return x.len()

and to support len() a class must define method len()?

So why not require every object to define method len() and get rid of len() builtin?

Is there any benefit to len(x) over x.len()?

(at the very least: x.len() removes an extra level of indirection)

E.g python does not have read() builtin (which just calls read method), instead you simply define a read()method when you need it

1

u/[deleted] Aug 31 '08

Conceptually, it is just calling __len__(), although for sequence-like internal objects there is a different mechanism tried if no explicit length interface is defined. In the case of len(), I would probably prefer that it be an instance method.

Part of the reason for that is that I was wrong about len() taking iterables. If the object doesn't define __len__() and isn't a "sized" type (an internal implementation detail concerning tuples and strings), the operation fails. It doesn't "follow" iterators to count how many things they yield. So unlike map, reduce, filter, et al., it's not a generic high-level utility (even if a simple one) so much as a stub. I imagine that it is around primarily for historical reasons, and now that a __len__() interface exists, it is very hard to get rid of.

One thing I will say in its favor is that it makes sequences easier to work with in C modules since __len__() is much more clearly defined in that context than "some random instance method with a pseudo-standardized name", and the internal version of len(), called PyObject_Size(), returns a C integer instead of a PyObject*. Even if you call len() in Python against a pure-python __len__(), a TypeError is raised if the function doesn't return an integer.