[Tutor] the class struggle
alan.gauld@bt.com
alan.gauld@bt.com
Mon, 3 Dec 2001 13:14:57 -0000
> 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.'
Can I suggest that a dictionary would simplfy this?
people = {}
for x in myList:
people[x] = Struggle(soviet,x)
wasCommunist = people[x].Communist
wasSoviet = people[x].Soviet
if wasCommunist:
print x, ' was a communist'
if wasSoviet:
print x, ' was a soviet leader'
You can either hold onto people for long term reference
or just delete the entries as you go.
Maybe it's just me but all those evals hurt my head!
Alan G.