setting variables from a tuple NEWB

M

manstey

Hi,

If I have a tuple like this:

tupGlob = (('VOWELS','aeiou'),('CONS','bcdfgh'))

is it possible to write code using tupGlob that is equivalent to:
VOWELS = 'aeiou'
CONS = ''bcdfgh'

Thanks,
Matthew
 
M

Marc 'BlackJack' Rintsch

tupGlob = (('VOWELS','aeiou'),('CONS','bcdfgh'))
for nam,val in tupGlob: locals()[nam]=val ...
VOWELS 'aeiou'
CONS 'bcdfgh'

Little warning: It works only on module level as assigning to `locals()`
return value in functions and methods has no effect on the "real" locals.

Ciao,
Marc 'BlackJack' Rintsch
 
N

nate

manstey said:
Hi,

If I have a tuple like this:

tupGlob = (('VOWELS','aeiou'),('CONS','bcdfgh'))

is it possible to write code using tupGlob that is equivalent to:
VOWELS = 'aeiou'
CONS = ''bcdfgh'

could you use a dictionary instead? i.e.
tupGlob = {'VOWELS':'aeiou', 'CONS':'bcdfgh'}
tupGlob['VOWELS'] 'aeiou'
tupGlob['VOWELS'] = 'aeiou AndSometimesY'
tupGlob['VOWELS']
'aeiou AndSometimesY'

nate
 
S

Steven D'Aprano

Hi,

If I have a tuple like this:

tupGlob = (('VOWELS','aeiou'),('CONS','bcdfgh'))

is it possible to write code using tupGlob that is equivalent to:
VOWELS = 'aeiou'
CONS = ''bcdfgh'

Why don't you just do that?

VOWELS = 'aeiou'
CONS = 'bcdfgh'

What benefit do you gain by stuffing your variables into tuples?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top