Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #76780 > unrolled thread

when the method __get__ will be called?

Started byluofeiyu <elearn2014@gmail.com>
First post2014-08-22 15:08 +0800
Last post2014-08-22 17:30 +1000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  when the method  __get__  will be called? luofeiyu <elearn2014@gmail.com> - 2014-08-22 15:08 +0800
    Re: when the method  __get__  will be called? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-22 17:30 +1000

#76780 — when the method __get__ will be called?

Fromluofeiyu <elearn2014@gmail.com>
Date2014-08-22 15:08 +0800
Subjectwhen the method __get__ will be called?
Message-ID<mailman.13278.1408691339.18130.python-list@python.org>
class C(object):
     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)


 >>> x=C()
 >>> x.a
__getattribute__() is called
'abc'
 >>> x.b
__getattribute__() is called
__getattr__() is called
'b from getattr'




If call an attribute which does exist ,__getattribute__() is called
If call an attribute which does not  exist ,__getattribute__() is called
and then __getattr__() is called ?

when the __get__ method will be called?no chance for my example?

[toc] | [next] | [standalone]


#76782

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-08-22 17:30 +1000
Message-ID<53f6f19a$0$29978$c3e8da3$5496439d@news.astraweb.com>
In reply to#76780
Luofeiyu, you are asking very advanced questions. How experienced with
Python are you? Asking about __get__ is nearly never needed. __get__ is
used internally by built-ins like property, classmethod and staticmethod.
99.99% of Python programmers will never need to write a __get__ method, and
of the 0.01% who do, they might only write one or two in their whole
career.


luofeiyu wrote:

> class C(object):
>      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)


[...]
> If call an attribute which does exist ,__getattribute__() is called
> If call an attribute which does not  exist ,__getattribute__() is called
> and then __getattr__() is called ?

Correct.

 
> when the __get__ method will be called?no chance for my example?

No chance for your example. __get__ is used in the "descriptor protocol",
used for methods and properties. You can see __get__ called here:


class D(object):
    foo = C()

d = D()
d.foo


Now C.__get__ will be called.



-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web