Question on for loop

Don Ross donrosszx at gmail.com
Thu Jan 3 18:22:38 EST 2013


I'm interested to know why you're trying this as well.  Is this something that would be helped by creating a class and then dynamically creating instances of that class?  Something like...

class Fruit:
    def __init__(self, name):
        self.name = name

for fruit in ['banana', 'apple', 'mango']:
    varName = Fruit(fruit)
    # do stuff with varName

On Thursday, January 3, 2013 2:04:03 PM UTC-6, subhaba... at gmail.com wrote:
> Dear Group,
> 
> If I take a list like the following:
> 
> 
> 
> fruits = ['banana', 'apple',  'mango']
> 
> for fruit in fruits:        
> 
>    print 'Current fruit :', fruit
> 
> 
> 
> Now, 
> 
> if I want variables like var1,var2,var3 be assigned to them, we may take,
> 
> var1=banana,
> 
> var2=apple,
> 
> var3=mango
> 
> 
> 
> but can we do something to assign the variables dynamically I was thinking
> 
> of 
> 
> var_series=['var1','var2','var3']
> 
> for var in var_series:
> 
>   for fruit in fruits:
> 
>        print var,fruits
> 
> 
> 
> If any one can kindly suggest.
> 
> 
> 
> Regards,
> 
> Subhabrata
> 
> 
> 
> NB: Apology for some alignment mistakes,etc.




More information about the Python-list mailing list