call to function by text variable

Cameron Laird claird at lairds.us
Mon Mar 26 00:30:18 EDT 2007


In article <mailman.5604.1174863631.32031.python-list at python.org>,
Jan Schilleman <jan.schilleman at xs4all.nl> wrote:
>Hi,
>
>try this:
>func = getattr(operations, ["Replace", "ChangeCase", "Move"][n])
>
>HTH,
>Jan
>
>"ianaré" <ianare at gmail.com> schreef in bericht 
>news:1174862186.134912.117270 at p15g2000hsd.googlegroups.com...
>> 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))
			.
			.
			.
I think you meant "...[n - 1]" rather than "...[n]".

I'm a tiny bit surprised no one has organized this in terms
of a dictionary.  I don't know, of course, how robust is the
characterization of n as a small integer.  Maybe

  lookup_table = {
      0: "Replace",
      1: "ChangeCase",
      2: "Move"}

captures the sentiment; maybe something else does it better.



More information about the Python-list mailing list