Is this a super bug?

Michele Simionato mis6 at pitt.edu
Mon Apr 21 17:50:00 CEST 2003


"Bjorn Pettersen" <BPettersen at NAREX.com> wrote in message news:<mailman.1050817233.1530.python-list at python.org>...
> > From: Bjorn Pettersen 
> 
> Hate to reply to myself, but...
> 
> If this isn't the   getattribute   bug I read about, it needs to be
> documented that super only supports  direct  access to attributes, but
> not properties (although the Python version does), of a class in the
>   mro  , but will not access any attribute the language defines as
> "hooks" for various functionality indirectly... (i.e. all the   xx  
> methods -- I could understand   getattribute   would be a problem, but
> even   getattr   should have been possible to implement).
> 
> Sort of a special purpose proxy for the superclass... not what I
> expected from the name. And there doesn't seem there is a way to
> implement it in Python either. If I understand correctly, these
> attributes are looked for directly in the instance's class, skipping
> lookup in the instance itself, which is why the super instance's
>   getattr   or   getattribute   aren't called. Question: are   getattr  
> and   getattribute   ever called on the metaclass? Why not/why not in
> this case?
> 
> -- bjorn
> 

I think you got confused, here. Nothing related to metaclasses.
You should give a look to the attributes of super(ff,f):

>>> super(ff,f)
['__class__', '__delattr__', '__doc__', '__get__', '__getattribute__',
'__hash__', '__init__', '__new__', '__reduce__', '__repr__',
'__self__', '__setattr__', '__str__', '__thisclass__']

You see that super(ff,f) has no string attributes and no __getattr__.
It has a non-trivial __getattribute__, however, a __get__ attribute 
(thus it is a descriptor) and two interesting attributes __self__ and
__thisclass__ with clear meaning.

The output of 'dir' in my implementation of Super is more misleading, 
since dir invokes __getattribute__ and retrieves the attribute of the
str class:

>>> Super(ff,f)
['__add__', '__class__', '__contains__', '__delattr__', '__dict__',
'__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__',
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__',
'__lt__', '__mod__', '__module__', '__mul__', '__ne__', '__new__',
'__reduce__', '__repr__', '__rmod__', '__rmul__', '__setattr__',
'__str__', '__weakref__', 'capitalize', 'center', 'count', 'decode',
'encode', 'endswith', 'expandtabs', 'find', 'foo', 'index', 'isalnum',
'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper',
'join', 'ljust', 'lower', 'lstrip', 'replace', 'rfind', 'rindex',
'rjust', 'rstrip', 'split', 'splitlines', 'startswith', 'strip',
'swapcase', 'title', 'translate', 'upper', 'zfill']

This is confusing, since actually my Super does not define all those 
methods, it is only a proxy to them, like the standard one. The 
dict of Super gives you the truth:

>>> for k,v in vars(type(Super(ff,f))).items(): print k,':',v #my
Super
__dict__ : <attribute '__dict__' of 'Super' objects>
__getattribute__ : <function __getattribute__ at 0x4032b764>
__weakref__ : <attribute '__weakref__' of 'Super' objects>
__doc__ : Invoked as Super(cls,obj).meth returns the supermethod of
    cls with respect to the MRO of obj. If obj is a subclass of cls,
    it returns the unbound supermethod of obj; otherwise, if obj is an
    instance of some subclass of cls, it returns the obj-bound method.
__init__ : <function __init__ at 0x4032b72c>

Somebody more knowledgeable than me should explain how the built-in
super
can override __getattribute__ without effects on the output of 'dir' !

It is also possible my understanding of how super is working is
incorrect;
I had much more trouble in understanding super the metaclasses ;)

Cheers,


                                                      Michele




More information about the Python-list mailing list