Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attribute': 0.07; 'none,': 0.07; '*args,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:method': 0.09; 'def': 0.12; '**kwargs)': 0.16; '**kwargs):': 0.16; 'a(object):': 0.16; 'descriptor': 0.16; 'example?': 0.16; 'fallback': 0.16; 'name):': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:when': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'instance,': 0.24; 'subject:will': 0.24; 'skip:_ 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'class': 0.32; 'skip:_ 10': 0.34; 'subject:the': 0.34; 'method': 0.36; 'subject:?': 0.36; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'called': 0.40; 'subject: ': 0.61; 'name': 0.63; 'chance': 0.65; 'x):': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: when the method __get__ will be called? Date: Fri, 22 Aug 2014 09:27 +0200 Organization: None References: <53F6EC69.6060609@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd815a.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1408692437 news.xs4all.nl 2936 [2001:888:2000:d::a6]:60678 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:76781 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) > > > >>> 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 ? You typically use either __getattribute__() which is called for every attribute or __getattr__() which is called only as a fallback when an attribute is not found in the instance __dict__. > when the __get__ method will be called?no chance for my example? __get__() is part of the descriptor protocol; it is called on attribute access: >>> class A(object): ... c = C() ... >>> A().c ('__get__() is called', <__main__.A object at 0x7f7cc2c8e850>, ) <__main__.C object at 0x7f7cc2c8e910> >>> A.c ('__get__() is called', None, ) <__main__.C object at 0x7f7cc2c8e910>