[Tutor] the class struggle
Fri, 30 Nov 2001 18:32:05 -0800 (PST)
On Fri, 30 Nov 2001, Rob McGee wrote:
> That is exactly what I mean. I simply pass the "name" variable to the
> class instantiation. I don't really even need to set a "self.name"
> variable -- for my purposes all I need is to evaluate that "name"
> parameter inside the __init__ function:
> soviet = ['Lenin', 'Trotsky', 'Stalin']
> class Struggle:
> def __init__(self, list, name):
> self.Communist = 1
> if name in list:
> self.Soviet = 1
> self.Soviet = 0
> myList = ['Marx', 'Engels', 'Lenin', 'Mao']
> for x in myList:
> execString = x + ' = Struggle(soviet, "' + x + '")'
> evalCommunist = x + '.Communist'
> evalSoviet = x + '.Soviet'
> if eval(evalCommunist):
> print x, 'was a known Communist.'
> if eval(evalSoviet):
> print x, 'was a Soviet leader.'
By the way, you don't need to use eval() so much. Instead of:
eval('x' + ".Soviet")
you can just use:
with the same effect.
Have a good weekend!