Creating variables in a class definition
Tage Stabell-Kulo
tage at cs.uit.no
Sun Dec 10 03:20:05 EST 2000
Here is how I would like to globally suck in the environment, and then
use that throughout the modules; notice the way I derive from DEMO so
that I can use self.ROOT.
class DEMO:
# Here are the variables we want to update
var = { "PATH" : "/bin", "ROOT" : "/var" }
for i in in a.keys():
tmp = os.getenv(i)
if tmp:
a[i] = tmp
# Create variables
PATH = var["PATH"]
ROOT = var["ROOT"]
class DoIt(DEMO):
def __init__(self):
os.chdir(self.ROOT)
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?
I known getattr will do it for me, but if I use getattr in
DEMO have I not inhibited myself from using getattr in DoIt (or
ended up with even more complexity than I had in the first place)?
Yours cincerely,
[TaSK]
More information about the Python-list
mailing list