To declare multiple variables at the "same time" I would do:
a, b = True, False
But if I had to declare much more variables, it turns less and less elegant:
a, b, c, d, e, f, g, h, i, j = True, True, True, True, True, False, True ,True , True, True
Is there a better / elegant / convenient way to do this?
Thanks in advance!
Edit:
This must be very basic, but if I do used a list or a tuple for storing the variables, how would I have to approach so that I would be helpful since:
aList = [a,b]
Is not valid, I would have to do:
a, b = True, True
Or what am I missing?
What's the problem , in fact ?
If you really need or want 10 a, b, c, d, e, f, g, h, i, j , there will be no other possibility, at a time or another, to write a and write b and write c.....
If the values are all different, you will be obliged to write for exemple
that is to say defining the "variables" individually.
Or , using another writing, no need to use
_
:or
.
If some of them must have the same value, is the problem that it's too long to write
?
Then you can write:
.
I don't understand what is exactly your problem. If you want to write a code, you're obliged to use the characters required by the writing of the instructions and definitions. What else ?
I wonder if your question isn't the sign that you misunderstand something.
When one writes
a = 10
, one don't create a variable in the sense of "chunk of memory whose value can change". This instruction:either triggers the creation of an object of type
integer
and value 10 and the binding of a name 'a' with this object in the current namespaceor re-assign the name 'a' in the namespace to the object 10 (because 'a' was precedently binded to another object)
I say that because I don't see the utility to define 10 identifiers a,b,c... pointing to False or True. If these values don't change during the execution, why 10 identifiers ? And if they change, why defining the identifiers first ?, they will be created when needed if not priorly defined
Your question appears weird to me
When people are suggesting "use a list or tuple or other data structure", what they're saying is that, when you have a lot of different values that you care about, naming them all separately as local variables may not be the best way to do things.
Instead, you may want to gather them together into a larger data structure that can be stored in a single local variable.
intuited showed how you might use a dictionary for this, and Chris Lutz showed how to use a tuple for temporary storage before unpacking into separate variables, but another option to consider is to use
collections.namedtuple
to bundle the values more permanently.So you might do something like:
Real code would hopefully use more meaningful names than "DataHolder" and the letters of the alphabet, of course.