473,511 Members | 12,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confusion about __call__ and attribute lookup

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
Nov 10 '05 #1
5 1353
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'
Nov 10 '05 #2
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
Nov 10 '05 #3
Kent Johnson <ke****@tds.net> 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

Nov 22 '05 #4
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

Nov 22 '05 #5
John J. Lee wrote:
Kent Johnson <ke****@tds.net> 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 do**@python.org

Thanks to both of you,
Kent
Nov 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2483
by: Patrick Lioi | last post by:
def foo(): pass foo is a function foo is a callable object foo has method __call__ defined foo.__call__ is a function foo.__call__ is a callable object foo.__call__ has method __call__...
5
2358
by: Robert Ferrell | last post by:
I have a question about assigning __call__ to an instance to make that instance callable. I know there has been quite a bit of discussion about this, and I've read all I can find, but I'm still...
1
1388
by: Holger Joukl | last post by:
Hi there, please excuse my rather lengthy post. With introduction of the new style classes, something seems to have changed for __getattr__ hooks, even for classic classes: getattr.py: class...
11
1855
by: Stefan Behnel | last post by:
Hi! This somewhat puzzles me: Python 2.4 (#1, Feb 3 2005, 16:47:05) on linux2 Type "help", "copyright", "credits" or "license" for more information. ..>>> class test(object): .... def...
8
1700
by: Rahul | last post by:
Consider the following: def a(x): return x+1 def b(f): def g(*args,**kwargs): for arg in args: print arg return f(*args,**kwargs) return g
0
1404
by: Carl | last post by:
I want to create a generic xslt that would take xml input like: ########################################################################## <?xml version="1.0" encoding="utf-8" ?>...
0
2172
by: Carl | last post by:
Hi, I have found a way to map attributes (columns) to column headings. But this runs really slow. Is there a way to improve it? Thanks, Carl <?xml version="1.0" encoding="utf-8" ?>...
6
3535
by: Adam Donahue | last post by:
As an exercise I'm attempting to write a metaclass that causes an exception to be thrown whenever a user tries to access 'attributes' (in the traditional sense) via a direct reference. Consider:...
4
1418
by: MonkeeSage | last post by:
Proposal: When an attribute lookup fails for an object, check the top-level (and local scope?) for a corresponding function or attribute and apply it as the called attribute if found, drop...
0
7251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7148
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7089
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.