the output in reference of descriptor.

CHIN Dihedral dihedral88888 at gmail.com
Sat Aug 23 01:44:13 EDT 2014


On Friday, August 22, 2014 9:25:02 AM UTC+8, luofeiyu wrote:
> class C(object):
> 

Well, in python class is treated 
as onte of the first class built in
operations.

class "new_class_ame" ( parentclasses)


Please check this syntax first in Python.
>          a = 'abc'
> 
>          def __getattribute__(self, *args, **kwargs):
> 
>              print("__getattribute__() is called")
> 
>              return object.__getattribute__(self, *args, **kwargs)
> 
>          def __getattr__(self, name):
> 
>              print("__getattr__() is called ")
> 
>              return name + " from getattr"
> 
>          def __get__(self, instance, owner):
> 
>              print("__get__() is called", instance, owner)
> 
>              return self
> 
>          def foo(self, x):
> 
>              print(x)
> 
> 
> 
> 
> 
>      class C2(object):
> 
>          d = C()
> 
> 
> 
> 
> 
>  >>> c2.d
> 
> __get__() is called <__main__.C2 object at 0x000000000297BE10> <class 
> 
> '__main__.
> 
> C2'>
> 
> <__main__.C object at 0x000000000297BBA8>
> 
> 
> 
> I understant the result ,c2.d trigger the __get__ method in class C.
> 
>      def __get__(self, instance, owner):
> 
>          print("__get__() is called", instance, owner)
> 
>          return self
> 
> 
> 
> It print "__get__() is called", instance, owner and return self 
> 
> `<__main__.C object at 0x000000000297BBA8>`
> 
> 
> 
> 
> 
>  >>> c2.d.a
> 
> __get__() is called <__main__.C2 object at 0x000000000297BE10> <class 
> 
> '__main__.
> 
> C2'>
> 
> __getattribute__() is called
> 
> 'abc'
> 
> 
> 
> Why the result of c2.d.a  is not :
> 
> 
> 
> __get__() is called <__main__.C2 object at 0x000000000297BE10> <class 
> 
> '__main__.
> 
> C2'>
> 
> __getattribute__() is called
> 
> 'abc'
> 
> 
> 
> Why the` return self` in the __get__ method in class C  does not work?




More information about the Python-list mailing list