call to function by text variable

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun Mar 25 18:51:26 EDT 2007


En Sun, 25 Mar 2007 19:36:26 -0300, ianaré <ianare at gmail.com> escribió:

> list = ["Replace", "ChangeCase", "Move"]
> textVariable = list[n]
> self.operations.insert(pos, operations.[textVariable].Panel(self,
> main))
>
> Is something sort of like that possible?

Try getattr:
textVariable = "Replace"
getattr(operations, textVariable) == operations.Replace

Perhaps you could just store the result in your list; instead of  
["Replace", "ChangeCase", "Move"], use [operations.Replace,  
operations.ChangeCase, operations.Move] (it's not always applicable, of  
course: maybe operations is reassigned, or those attributes mutate or  
don't always exist...)

-- 
Gabriel Genellina




More information about the Python-list mailing list