Re: dynamic call of a function

Discussion in 'Python' started by kishore, Jan 22, 2005.

  1. kishore

    kishore Guest

    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
    kishore, Jan 22, 2005
    #1
    1. Advertising

  2. kishore

    Nick Coghlan Guest

    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
    Nick Coghlan, Jan 23, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. grbgooglefan
    Replies:
    2
    Views:
    408
    Pascal Bourguignon
    Jan 30, 2008
  2. grbgooglefan
    Replies:
    4
    Views:
    428
    Kenny McCormack
    Jan 30, 2008
  3. grbgooglefan
    Replies:
    0
    Views:
    381
    grbgooglefan
    Jan 30, 2008
  4. Alok
    Replies:
    3
    Views:
    237
  5. THAKUR PRASHANT SINGH

    Class Function call vs Normal Function call

    THAKUR PRASHANT SINGH, Feb 26, 2010, in forum: Ruby
    Replies:
    7
    Views:
    170
    THAKUR PRASHANT SINGH
    Feb 27, 2010
Loading...

Share This Page