Confusion about __call__ and attribute lookup

Discussion in 'Python' started by Kent Johnson, Nov 10, 2005.

  1. Kent Johnson

    Kent Johnson Guest

    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
     
    Kent Johnson, Nov 10, 2005
    #1
    1. Advertising

  2. 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'
     
    Leif K-Brooks, Nov 10, 2005
    #2
    1. Advertising

  3. Kent Johnson

    Kent Johnson Guest

    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
     
    Kent Johnson, Nov 10, 2005
    #3
  4. Kent Johnson

    John J. Lee Guest

    Kent Johnson <> 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
     
    John J. Lee, Nov 13, 2005
    #4
  5. Kent Johnson

    Serge Orlov Guest

    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/python-dev/2003-May/035732.html
     
    Serge Orlov, Nov 13, 2005
    #5
  6. Kent Johnson

    Kent Johnson Guest

    John J. Lee wrote:
    > Kent Johnson <> 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

    Thanks to both of you,
    Kent
     
    Kent Johnson, Nov 14, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick Lioi
    Replies:
    7
    Views:
    363
    Beni Cherniavsky
    Aug 19, 2003
  2. chenyu
    Replies:
    1
    Views:
    315
  3. andrew cooke

    ABCs, functions, and __call__ (Python3)

    andrew cooke, Jan 17, 2009, in forum: Python
    Replies:
    1
    Views:
    319
    andrew cooke
    Jan 17, 2009
  4. Josh Cheek

    Constant Lookup Confusion

    Josh Cheek, Oct 19, 2010, in forum: Ruby
    Replies:
    2
    Views:
    88
    Josh Cheek
    Oct 19, 2010
  5. Marco
    Replies:
    1
    Views:
    192
    Ian Kelly
    Nov 20, 2012
Loading...

Share This Page