dictionary with object's method as thier items

Gabriel Genellina gagsl-py at yahoo.com.ar
Wed Aug 30 11:26:24 EDT 2006


At Wednesday 30/8/2006 10:35, noro wrote:

>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, such as obj with
>sometiing like:
>(i know that this syntax wont work )
>
>obj.(d['function one'])
>obj.(d['function two'])

You can use dir(obj) to get its list of attributes (including method 
names) then use getattr to invoke the method.

methodname='func1'
getattr(obj,methodname)()

See the inspect module too.


Gabriel Genellina
Softlab SRL 


	
	
		
__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya! 
http://www.yahoo.com.ar/respuestas




More information about the Python-list mailing list