[Tutor] For Loop Question
Mitya Sirenef
msirenef at lightbird.net
Mon Dec 24 02:16:33 CET 2012
On 12/23/2012 08:03 PM, Tom Tucker wrote:
>
>
> Python Gurus,
> I got a question for ya. Below I have three instance variables
> (self.A, self.B, etc). How can I use the below
> for loop for A, B, C to also call those instance variables?
>
> Example
> ###############
> <some code>
> .....
> .....
> self.A = 1
> self.B = 2
> self.C = 3
>
> myDict = {'A': 1, 'B': 2, 'C': 1}
> for x in [A, B, C]:
>
> if myDict[x] == (self.%s % (x)): # Invalid if statement.. ;-)
> print "Yep, we have a match!"
> else:
> print "Sorry..No match!"
>
>
If I understood you right, you can do: myDict[x] == getattr(self, x)
- mitya
--
Lark's Tongue Guide to Python: http://lightbird.net/larks/
More information about the Tutor
mailing list