cooperation of buitlin methods usingtsuper

Matthias Oberlaender matthias.oberlaender at VOID.daimlerchrysler.com
Fri Jul 4 09:57:35 EDT 2003


In <2259b0e2.0307040511.1eb91faa at posting.google.com> Michele Simionato wrote:
<snip>
> To add trickyness to trickyness, I show you a couple of examples where
> the same problems appear. These examples (or examples similar to them)
> where pointed out to me by Bjorn Pettersen. The real problem seems to be
> in the lookup rule for len(x) which is not always equivalent to
> x.__len__() when tricks with __getattr__ are performed:
> 
> First example: defining __len__ on the object does not work
> 
> class E(object):
>     def __getattr__(self,name):
>             if name=='__len__': return lambda:0
> 
> >>> e=E()
> >>> e.__len__()
> 0
> >>> len(e)
> Traceback ...
> 
> Second example: defining __len__ on the class does not work:
> 
> class M(type):
>     def __getattr__(self,name):
>             if name=='__len__': return lambda self:0
> 
> class F: __metaclass__=M
> 
> >>> f=F()
> >>> F.__len__(f) # okay
> 0
> >>> len(f) # TypeError: len() of unsized object
> f.__len__() # AttributeError: 'F' object has no attribute '__len__'
> 
> As you see, the problem is that len(x) is not calling x.__len__(),
> nor x.__class__.__len__(x); I really would like to know how ``len`` 
> (or ``str``, ``repr``, etc.) work: I think this is the core of
> the problem, whereas ``super`` is probably a red herring.
> 
>                                   Michele
> 
Yes, I very nuch support this question: How do the builtins work exactly? 
Where is all this documented?
Nevertheless, my impression remains that  builtins and super do not play 
consistently together. Again, there is not much documentation concerning 
super either. It's still listed under 'Built-in Functions' in section 21 of 
the Library Reference, but actually t's a type. 

Well, Python's evolutionary character is both its strength and weakness.

P.S. I will remain silent for the next couple of days. Thanks for the 
discussion so far. And I'll think about the idea of filing a (documentation) 
bug report as suggested by Michele. Would be my first one so I have to figure 
out how to do that. 

--
 ____  __  _/_/ . 
( / / ( /  / / /  

=====================================================================
Matthias Oberlaender, DaimlerChrysler AG, Research Center Ulm
RIC/AP (Machine Perception)
Wilhelm-Runge-Str. 11,  P.O. Box 2360,  89013 Ulm, Germany
Phone: +49 731 505 2354       Fax: +49 731 505 4105
Email: matthias.oberlaender at REMOVE.daimlerchrysler.com
=====================================================================





More information about the Python-list mailing list