dictionary with object's method as thier items

Roberto Bonvallet rbonvall at cern.ch
Wed Aug 30 10:23:35 EDT 2006


noro wrote:
> Is it possible to do the following:
> 
> for a certain class:
[...]
> by some way create a dictionary that look somthing like that:
> 
> d= {'function one': <reference to C.func1()>, \
>      'function two': <reference to C.func2()>, \
>      'function three': <reference to C.func3()>}
> 
> and so i could access every method of instances of C

Something like this?

>>> class C:
...     def f1(self):
...         print "i'm one"
...     def f2(self):
...         print "i'm two"
...
>>> obj = C()
>>> d = {'one': obj.f1, 'two': obj.f2}
>>> d['one']()
i'm one
>>> d['two']()
i'm two

-- 
Roberto Bonvallet



More information about the Python-list mailing list