Acceptance test spike example

Terry Reedy tjreedy at udel.edu
Sun Jun 26 22:42:40 EDT 2005


"Steve Jorgensen" <nospam at nospam.nospam> wrote in message 
news:mhcub1p3mlcbnsf0dqk9akdrm5ut347rd0 at 4ax.com...
> Note how the powerful, context-aware exec() and eval() procedures really 
> help
> simplify the code.

A stylistic note:  I believe that most or all of your eval/exec uses could 
be done with getattr and setattr instead, which are in the language for 
precisely those situations in which the name of an attribute is in a 
runtime string.  To my mind, this would be simpler and better style.

>        valueExpr = "self.model." + parsed.name
>        valueIs = eval(valueExpr)

I believe this is valueIs = getattr(self.model, parsed.name)

>        methodCall = "self.model." + parsed.name +"(" + parsed.value + ")"
>        exec(methodCall)


I believe this is getattr(self.model, parsed.name)(parsed.value).


>        exec("self.model." + parsed.name + "=" + parsed.value)

I believe this is setattr(self.model, parsed.name, parsed.value).

and so on.

Terry J. Reedy






More information about the Python-list mailing list