call to function by text variable

Steve Holden steve at holdenweb.com
Sun Mar 25 18:48:19 EDT 2007


ianaré 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.
> 
> like this:
> 
> 
> list = ["Replace", "ChangeCase", "Move"]
> textVariable = list[n]
> self.operations.insert(pos, operations.[textVariable].Panel(self,
> main))
> 
> Is something sort of like that possible?
> 
Indeed. You don't need to use textual names (though for that you can 
investigate "getattr()) - the following, naturally, is untested:

ops = [operations.Replace,
        operations.ChangeCase,
        operations.Move
       ]
self.operations.insert(pos, ops[n-1].Panel(self, main)

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb     http://del.icio.us/steve.holden
Recent Ramblings       http://holdenweb.blogspot.com




More information about the Python-list mailing list