Acceptance test spike example

Steve Jorgensen nospam at nospam.nospam
Sun Jun 26 23:12:49 EDT 2005


On Sun, 26 Jun 2005 22:42:40 -0400, "Terry Reedy" <tjreedy at udel.edu> wrote:

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

Thanks.  I'm new to Python, so I'll take all the style advice I can get.

- Steve J.



More information about the Python-list mailing list