call to function by text variable

attn.steven.kuo at gmail.com attn.steven.kuo at gmail.com
Sun Mar 25 18:52:13 EDT 2007


On Mar 25, 3:36 pm, "ianaré" <ian... at gmail.com> wrote:
> yeah the subject doesn't really make sense does it?
>
> anyway want I want to do is this:
> if n == 1:
>
>     self.operations.insert(pos, operations.Replace.Panel(self, main))
>
> elif n == 2:
>
>     self.operations.insert(pos, operations.ChangeCase.Panel(self,
> main))
>
> elif n == 3:
>
>     self.operations.insert(pos, operations.Move.Panel(self, main))
>
> As you can see all the different functions have the same variables, so
> it would be easier if I could just make a list and use that.
>


# Your list would contain the unbound functions:

unbound_funcs = [operations.Replace.Panel,
    operations.Change.Panel,
    operations.Move.Panel]


# and invocation would be:

self.operations.insert(pos, unbound_funcs[n - 1](self, main))


--
Hope this helps,
Steven





More information about the Python-list mailing list