Pb with descriptor and super ...

Erwan Adam erwan.adam at cea.fr
Fri Mar 9 05:41:45 EST 2007


Erwan Adam a écrit :
> Hi all,
> 
> I have a problem with the use of descriptors
> and super. The file is :
> 
> # ---------------
> 
> class Desc(object):
>     def __init__(self, class_name):
>         self.class_name = class_name
>         return
>     def __get__(self, obj, typ):
>         print "Desc.__get__ : class_name is %s, obj is %s and typ is 
> %s"%(self.class_name, obj, typ)
>         return
>     pass
> 
> class A(object):
>     attr = Desc("A")
>     pass
> 
> class B(A):
>     attr = Desc("B")
>     pass
> 
> b = B()
> 
> print "-------"
> print "Getting b.attr ..."
> print "I hope that  : class_name is B, obj is b and typ is B"
> b.attr
> print "-------"
> print "Getting super(B, b).attr ..."
> print "I hope that  : class_name is A, obj is b and typ is A but it is 
> not the case !"
> super(B, b).attr
> print "-------"
> 
> # -----------
> 
> and the result is :
> 
> -------
> Getting b.attr ...
> I hope that  : class_name is B, obj is b and typ is B
> Desc.__get__ : class_name is B, obj is <__main__.B object at 0xb7b1f8ec> 
> and typ is <class '__main__.B'>
> -------
> Getting super(B, b).attr ...
> I hope that  : class_name is A, obj is b and typ is A but it is not the 
> case !
> Desc.__get__ : class_name is A, obj is <__main__.B object at 0xb7b1f8ec> 
> and typ is <class '__main__.B'>
> -------
> 
> I expected that when getting super(B, b).attr, typ is A, but it is not
> the case ...

Moreover, on the page :

http://users.rcn.com/python/download/Descriptor.htm

the author writes :

"The call super(B, obj).m() searches obj.__class__.__mro__ for the base 
class A immediately following B and then returns 
A.__dict__['m'].__get__(obj, A)."

which it seems not the case ...

Is it a bug or a mis-understood ?

Regards,

          E.A.


> 
> python used :
> 
> [adam at is111902 /home/adam/Work/Python]> python
> Python 2.4.3 (#2, Sep 18 2006, 21:07:35)
> [GCC 4.1.1 20060724 (prerelease) (4.1.1-3mdk)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>  >>>
> 
> 
> Best regards,
> 
> 
>           E.A.



More information about the Python-list mailing list