Re: dynamic call of a function
From: kishore (prakis_at_gmail.com)
Date: 01/22/05
- Next message: Craig Ringer: "Re: need help on need help on generator..."
- Previous message: Alex Martelli: "Re: Class introspection and dynamically determining function arguments"
- Next in thread: Nick Coghlan: "Re: dynamic call of a function"
- Reply: Nick Coghlan: "Re: dynamic call of a function"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 22 Jan 2005 03:40:57 -0800
Hi Luigi Ballabio,
Thankyou very much for your reply,
it worked well.
Kishore.
Luigi Ballabio wrote:
> At 10:37 AM 10/19/01 +0200, anthony harel wrote:
> >Is it possible to make dynamic call of a function whith python ?
> >
> >I have got a string that contains the name of the function I
> >want to call but I don't want to do something like this :
> >
> >if ch == "foo" :
> > self.foo( )
> >elif ch == "bar"
> > self.bar( )
> >....
>
> Anthony,
> here are two ways to do it---I don't know which is the best,
nor
> whether the best is yet another. Also, you might want to put in some
error
> checking.
>
> class Test:
> def foo(self):
> print 'Norm!'
> def bar(self):
> print 'Dum-de-dum'
> def dynCall1(self,methodName):
> eval('self.%s()' % methodName)
> def dynCall2(self,methodName):
> method = vars(self.__class__)[methodName]
> method(self)
>
> >>> t = Test()
> >>> t.dynCall1('foo')
> Norm!
> >>> t.dynCall2('bar')
> Dum-de-dum
>
> Bye,
> Luigi
- Next message: Craig Ringer: "Re: need help on need help on generator..."
- Previous message: Alex Martelli: "Re: Class introspection and dynamically determining function arguments"
- Next in thread: Nick Coghlan: "Re: dynamic call of a function"
- Reply: Nick Coghlan: "Re: dynamic call of a function"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|