[Edu-sig] re: Naming in Python
Rodrigo Dias Arruda Senra
rsenra at acm.org
Sat Jan 29 20:11:53 CET 2005
[ The Bauman Family <baumanpdx at comcast.net> ]
-----------------------------------------------
| Although everybody already knows this, I'm sure, a function actually is
| a class,
Hi Tim,
I beg to differ with your statement above. A function is not a class.
To prove this point follows:
>>> def m(x): pass
>>> class M(object): pass
>>> type(m)
<type 'function'>
>>> type(M)
<type 'type'>
>>>
Altough they have similarities. To illustrate the most obvious one:
>>> callable(m)
True
>>> callable(M)
True
| so technically it is a function if you have all the things you
| get if you dir() a function.
Perhaps this is a duck typing style of defining what
an object *is*.
But a function is (OO framework sense) definetely an object:
>>> isinstance(m,object)
True
Therefore, a function supports the object protocol:
>>> x = object()
>>> dir(x)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__',
'__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__str__']
| >>> afunction.__repr__.__repr__.__repr__.__repr__()
| '<method-wrapper object at 0x41152c0c>'
|
| I wonder how Python deals with this endless chain of __repr__'s.
>>> m
<function m at 0x402c2a04>
This is the function object.
>>> m.__repr__()
'<function m at 0x402c2a04>'
This is the execution of method __repr__() bound to the function object m
>>> m.__repr__
<method-wrapper object at 0x402c870c>
m.__repr__ is an object (a method-wrapper) representing the method called above.
>>> m.__repr__.__repr__
<method-wrapper object at 0x402c878c>
Every object supports __repr__, even method-wrapper objects.
HTH,
best regards,
Rod Senra
--
,_
| ) Rodrigo Senra <rsenra |at| acm.org>
|(______ -----------------------------------------------
_( (|__|] GPr Sistemas http://www.gpr.com.br
_ | (|___|] IC - Unicamp http://www.ic.unicamp.br/~921234
___ (|__|]
L___(|_|] -----------------------------------------------
More information about the Edu-sig
mailing list