[Tutor] the class struggle

Danny Yoo dyoo@hkn.eecs.berkeley.edu
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:
> 
> {code}
> soviet = ['Lenin', 'Trotsky', 'Stalin']
> 
> class Struggle:
>   def __init__(self, list, name):
>     self.Communist = 1
>     if name in list:
>       self.Soviet = 1
>     else:
>       self.Soviet = 0
> 
> myList = ['Marx', 'Engels', 'Lenin', 'Mao']
> 
> for x in myList:
>   execString = x + ' = Struggle(soviet, "' + x + '")'
>   exec(execString)
>   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:

    x.Soviet

with the same effect.

Have a good weekend!