Avoiding `exec', how to?

holger krekel pyth at devel.trillke.net
Thu May 30 17:29:44 EDT 2002


Fran?ois Pinard wrote:
> [holger krekel]
> 
> >    Also the use of dir/getattr allows for 'defs' to be subclassed.
> >    Using '__dict__' doesn't and is somewhat ugly.
> 
> Hello, Holger.
> 
> I do not well understand your argument here -- but I agree that __dict__ is
> not fully nice.  What is the problem of not allowing `defs' to be subclassed?

Well. If 'defs' denotes a class you may *later* decide to have another
set 'defs2' which is based on 'defs' but modifies some values...
you code wouldn't break if you use 'dir(defs)'. Or if you later decide
you want to have an instance of 'defs' to be modified on instance level like

 mydefs = defs()
 mydefs.name1=value1 # etc.pp.

'dir(defs)' and 'getattr(defs, name) would still work well. 

It's good practice to make code more robust for such changes
in the future. It's also better for reusing it.

have fun,

    holger





More information about the Python-list mailing list