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


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

Re: the output in reference of descriptor.

Started byluofeiyu <elearn2014@gmail.com>
First post2014-08-24 11:23 +0800
Last post2014-08-24 17:18 +1000
Articles 2 — 2 participants

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: the output in reference of descriptor. luofeiyu <elearn2014@gmail.com> - 2014-08-24 11:23 +0800
    Re: the output in reference of descriptor. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-24 17:18 +1000

#76921 — Re: the output in reference of descriptor.

Fromluofeiyu <elearn2014@gmail.com>
Date2014-08-24 11:23 +0800
SubjectRe: the output in reference of descriptor.
Message-ID<mailman.13371.1408850628.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

let me paste it again to make my question more clear:

     >>>c2.d
     __get__() is called <__main__.C2 object at 0x000000000297BE10> 
<class '__main__.C2'>
     <__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'>
     <__main__.C object at 0x000000000297BBA8>
     __getattribute__() is called
     'abc'

Why the` return self` in the __get__ method in class C  does not work? 
Why there is no <__main__.C object at 0x000000000297BBA8>  in the output?


and the source code are:


|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)


class C2(object):
     d = C()


|


On 8/23/2014 12:10 PM, Ian Kelly wrote:
> On Thu, Aug 21, 2014 at 7:25 PM, luofeiyu <elearn2014@gmail.com 
> <mailto:elearn2014@gmail.com>> wrote:
> > >>> 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'
>
> As far as I can tell you pasted the same output twice, so I don't 
> understand what it is that you're asking.
>
>
>
>

[toc] | [next] | [standalone]


#76927

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-08-24 17:18 +1000
Message-ID<53f991b6$0$30000$c3e8da3$5496439d@news.astraweb.com>
In reply to#76921
luofeiyu wrote:

> let me paste it again to make my question more clear:
> 
>      >>>c2.d
>      __get__() is called <__main__.C2 object at 0x000000000297BE10>
> <class '__main__.C2'>
>      <__main__.C object at 0x000000000297BBA8>

You have an instance c2. You do an attribute lookup on d, which is a
descriptor, so d.__get__ is called. d.__get__ prints a status line, then
returns d's "self", which is an instance of C, not C2.

Because you are in the interactive interpreter, the returned result is
printed. Otherwise, it would be ignored.


>      >>> c2.d.a
>      __get__() is called <__main__.C2 object at 0x000000000297BE10>
> <class '__main__.C2'>
>      __getattribute__() is called
>      'abc'

This is the same as:

temp = c2.d
print(temp.a)

which is the same as:

temp = C()
print(temp.a)


> Why the result of c2.d.a  is not :
> 
>      __get__() is called <__main__.C2 object at 0x000000000297BE10>
> <class __main__.C2'>
>      <__main__.C object at 0x000000000297BBA8>
>      __getattribute__() is called
>      'abc'
> 
> Why the` return self` in the __get__ method in class C  does not work?

Of course it works. It returns the instance of C, then Python looks up
attribute "a" on that instance.


> Why there is no <__main__.C object at 0x000000000297BBA8>  in the output?

Because you didn't print it. If you want to see that, you can do:

temp = c2.d
print(temp)
print(temp.a)


In the interactive interpreter, but nowhere else, the final result of each
calculation is printed:

py> 1 + 2
3
py> 1 + 2 + 3 + 4 + 5
15

You should not expect it to do this:

py> 1 + 2 + 3 + 4 + 5
3
6
10
15


Only the last result is printed. The same applies for the . operator:

instance.a.b.c.d.e

will only print the final result, not

instance.a
instance.a.b
instance.a.b.c
instance.a.b.c.d
instance.a.b.c.d.e


-- 
Steven

[toc] | [prev] | [standalone]


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


csiph-web