Why does Python mix OO concepts and non OO concepts for operations on basic types?

Greg Ewing look at replyto.address.invalid
Wed May 22 23:58:43 EDT 2002


Michael Bauers wrote:
> 
> Why do you say x = []; x.append('a'), but get the length with len(a) ?
> 
> Is there a reason for this sort of inconsistency?

Something which hasn't been mentioned yet is that the
function-call style has some advantages when it comes
to adding new protocols to the language.

An example of this is the new iter() function. First
it looks to see if the object has an __iter__ method,
and if not, it falls back on older ways of iterating.
So, you can call iter() on any sequence object, old
or new, and it will do something sensible.

If the new protocol had been defined simply in
terms of a method, so that you said obj.iter()
instead of iter(obj), this would not have been
possible.

-- 
Greg Ewing, Computer Science Dept, 
University of Canterbury,	  
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list