Musings about Python syntax

Guido van Rossum guido at cnri.reston.va.us
Tue Oct 19 17:02:30 EDT 1999


Jim Althoff <jima at aspectdv.com> writes:

> At 07:03 PM 10/19/99 +0200, Fredrik Lundh wrote:
> >hey, what makes you think len(obj) is less object-
> >oriented than obj.len() ?  it's just syntax, you know.
> 
> But it IS less consistent syntax with not much advantage
> ("del" is a different story).  So it would be very nice if
> I could write aList.len() if I prefer that to len(aList).

I think of len() and other builtin functions as a special operators,
like + or ~ or not.  Using len(x) tells the reader more than using
x.len() -- len(x) is guaranteed to return an int and enforces that x
is a sequence; x.len() on the other hand cannot make such a guarantee:

class MyObject:
   def len(self): return "Spanish Inquisition"

--Guido van Rossum (home page: http://www.python.org/~guido/)




More information about the Python-list mailing list