Creating variables in a class definition

Alex Martelli aleaxit at yahoo.com
Sun Dec 10 09:34:37 EST 2000


"Tage Stabell-Kulo" <tage at cs.uit.no> wrote in message
news:90vebl$683$1 at news.uit.no...
    [snip]
> class DEMO:
>     # Here are the variables we want to update
>     var = { "PATH" : "/bin", "ROOT" : "/var" }
    [snip]
> The problem with this that the variables in DEMO are named twice; it
> is a ticking bomb waiting for someone to add one in the dictionary and
> forgetting to create a variable for it.
>
> The question: How to I create variables (from a dictionaly in this
> case) in the class as I initialize?

Other aspects of your code (that I've snipped) leave me somewhat
perplexed (what's "a", what does 'in in' mean, etc), but this one
is pretty straightforward: once you have your 'var' dictionary just
as you want it,
    __dict__.update(var)


Alex






More information about the Python-list mailing list