dynamic call of a function

Nick Coghlan ncoghlan at iinet.net.au
Sun Jan 23 00:37:59 EST 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')()

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. . .


Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia

More information about the Python-list mailing list