Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: dynamic call of a function

Reply
Thread Tools

Re: dynamic call of a function

 
 
kishore
Guest
Posts: n/a
 
      01-22-2005
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


 
Reply With Quote
 
 
 
 
Nick Coghlan
Guest
Posts: n/a
 
      01-23-2005
kishore wrote:
> Hi Luigi Ballabio,
>
> Thankyou very much for your reply,
> it worked well.


It does work in most cases, but "getattr(self, methodName)" is generally to be
preferred over "vars(self.__class__)[methodName]", as the latter does not use
Python's standard attribute lookup scheme.

The semantics of the following two statements are basically identical:
getattr(t, 'bar')()
t.bar()

Using vars() directly, however, results in a slightly different lookup process
that will *usually* give the same answer as above, but not always. It's that
'not always' which can end up hurting. . .

Regards,
Nick.

--
Nick Coghlan | | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to call function whose function call with arguments is in astring Options grbgooglefan C Programming 4 01-30-2008 05:12 PM
How to call function whose function call with arguments is in astring grbgooglefan C++ 2 01-30-2008 07:18 AM
How to call function whose function call with arguments is in astring Options grbgooglefan C Programming 0 01-30-2008 04:19 AM
defined? for recursive function call v/s defined? for function call stack Alok Ruby 3 04-13-2006 11:53 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57