Hard to understand 'eval'

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Mon Jun 16 05:11:33 EDT 2008


TheSaint a écrit :
> On 04:08, domenica 15 giugno 2008 bruno.desthuilliers at gmail.com wrote:
> 
>> what's wrong with getattr(cp, nn) ?
> 
> The learning curve to get into these programming ways.
> Does gettattr run the snippet passed in?

Nope, it just does what the name implies.

> Considering that nn is a name of function, which will be called and (cfl,
> value) are the parameters to passed to that function.

Everything in Python's an object (at least anything you can bind to a 
name), including functions and methods. Once you have a callable object, 
you just have to apply the call operator (parens) to call it. In your 
case, that would be:

func = getattr(cc, nn, None)
if callable(func):
   result = func(cfl, value)
else:
   do_whatever_appropriate_here()

> I'll spend some bit on getattr use.

Would be wise IMHO.



More information about the Python-list mailing list