Object Reference question

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Mon Aug 24 15:55:16 CEST 2009

josef a écrit :

 > I think that something like a = MyClass0(name =
> 'a', ...) is a bit redundant. Are definitions treated the same way?
> How would one print or pass function names?

In Python, classes and functions are objects too. The class and def 
statements are mostly syntactic sugar that *both* instanciate the (resp) 
class of function objet *and* bind it in the local namespace. FWIW, 
while class and function objects do (usually) have a __name__ attribute 
(usually the one used in the class or def statement...), this doesn't 
mean they're still bound to this name, nor that they are not bound to 
any other name:

bruno at bruno:~$ python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
pythonrc start
pythonrc done
 >>> def foo(): print "function foo ???"
 >>> foo
<function foo at 0x8563e9c>
 >>> foo.__name__
 >>> bar = foo
 >>> bar.__name__
 >>> bar()
function foo ???
 >>> import sys
 >>> foo = lambda: sys.stdout.write("pick a boo !\n")
 >>> foo.__name__
 >>> foo()
pick a boo !
 >>> funcs = [foo, bar]
 >>> del foo
 >>> funcs[0]
<function <lambda> at 0x8563ed4>
 >>> funcs[0]()
pick a boo !
 >>> funcs[1]
<function foo at 0x8563e9c>
 >>> funcs[1]()
function foo ???
 >>> funcs[1].__name__
 >>> del bar
 >>> funcs[1]()
function foo ???
 >>> funcs[1].__name__ = "yadda"
 >>> funcs[1]()
function foo ???
 >>> funcs[1].__name__


More information about the Python-list mailing list