Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Confusion about __call__ and attribute lookup

Reply
Thread Tools

Confusion about __call__ and attribute lookup

 
 
Kent Johnson
Guest
Posts: n/a
 
      11-10-2005
I am learning about metaclasses and there is something that confuses me.

I understand that if I define a __call__ method for a class, then instances of the class become callable using function syntax:

>>> class Foo(object):

... def __call__(self):
... print 'Called Foo'
...
>>> f=Foo()
>>> f()

Called Foo

To create a class instance, you call the class. This made me think that the class' class must define __call__, and indeed it does, and calling it as an unbound method also creates a class instance:

>>> dir(type)

[..., '__call__', ...]
>>> f=type.__call__(Foo)
>>> f

<__main__.Foo object at 0x00A35EB0>

But why doesn't Foo.__call__ shadow type.__call__? Normally an instance attribute takes precedence over a class attribute. Is it something special about how function call syntax is handled internally, or do all special methods work this way, or is there something else going on?

PS Is there any place in the standard Python docs where the details of attribute lookup are spelled out?

Thanks,
Kent
 
Reply With Quote
 
 
 
 
Leif K-Brooks
Guest
Posts: n/a
 
      11-10-2005
Kent Johnson wrote:
> But why doesn't Foo.__call__ shadow type.__call__? Normally an instance
> attribute takes precedence over a class attribute. Is it something
> special about how function call syntax is handled internally, or do all
> special methods work this way, or is there something else going on?


New-style classes look up special methods on the class, not on the instance:

>>> class Foo(object):

... def __invert__(self):
... return 'foo'
...
>>> x = Foo()
>>> ~x

'foo'
>>> x.__invert__ = 123
>>> x.__invert__()

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: 'int' object is not callable
>>> ~x # equivalent to type(x).__invert__()

'foo'
 
Reply With Quote
 
 
 
 
Kent Johnson
Guest
Posts: n/a
 
      11-10-2005
Leif K-Brooks wrote:
> New-style classes look up special methods on the class, not on the instance:


For my future reference, is this documented somewhere in the standard docs?

Thanks,
Kent
 
Reply With Quote
 
John J. Lee
Guest
Posts: n/a
 
      11-13-2005
Kent Johnson <(E-Mail Removed)> writes:

> Leif K-Brooks wrote:
> > New-style classes look up special methods on the class, not on the instance:

>
> For my future reference, is this documented somewhere in the standard docs?


Maybe somewhere in here

http://www.python.org/doc/newstyle.html


John

 
Reply With Quote
 
Serge Orlov
Guest
Posts: n/a
 
      11-13-2005
Kent Johnson wrote:
> Leif K-Brooks wrote:
> > New-style classes look up special methods on the class, not on the instance:

>
> For my future reference, is this documented somewhere in the standard docs?
>


Looks like it's the most detailed explanation on the net:

http://mail.python.org/pipermail/pyt...ay/035732.html

 
Reply With Quote
 
Kent Johnson
Guest
Posts: n/a
 
      11-14-2005
John J. Lee wrote:
> Kent Johnson <(E-Mail Removed)> writes:
>
>>Leif K-Brooks wrote:
>>
>>>New-style classes look up special methods on the class, not on the instance:

>>
>>For my future reference, is this documented somewhere in the standard docs?

>
> Maybe somewhere in here
>
> http://www.python.org/doc/newstyle.html


I have never found it there. I think something like the writeup Serge referenced should be in the language reference. I just sent the suggestion to http://www.velocityreviews.com/forums/(E-Mail Removed)

Thanks to both of you,
Kent
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The type.__call__() method manages the calls to __new__ and __init__? Marco Python 1 11-20-2012 07:25 PM
Constant Lookup Confusion Josh Cheek Ruby 2 10-19-2010 02:48 PM
ABCs, functions, and __call__ (Python3) andrew cooke Python 1 01-17-2009 03:45 AM
newbie question, when __call__ method is used? chenyu Python 1 10-27-2003 05:26 AM
Why doesn't __call__ lead to infinite recursion? Patrick Lioi Python 7 08-19-2003 06:41 PM



Advertisments