Friday Finking: initialising values and implied tuples
dn
PythonList at DancesWithMice.info
Fri Apr 2 18:10:18 EDT 2021
When there are several items to be defined and initialised, how do you
prefer to format the code, and why?
Apprentice: learn options
Journeyman: consider and discuss
Python Master: define, declare, and correct/advise/tutor
Some do not realise that using a tuple is a convenient way to convey
multiple values. Indeed if a function returns multiple values, that is
exactly what is happening, eg return x, y, z
Further, some think that the surrounding parentheses, ie ( surrounding
); 'define' the data-construct to be a tuple. This is not
completely-correct. In fact, it is the comma-separated list (in this
context) which identifies the data as a tuple - a 'tuple literal'.
https://docs.python.org/3/reference/expressions.html?highlight=literal%20tuple#parenthesized-forms
[yes, there'll be folk who decide that this *brief* intro should be the
discussion topic...]
The essence is to apply various values to a series of objects. Simple?
Using tuples can have its advantages, but as their length increases
humans have difficulties with the positional nature/relationship.
Here are a couple of ways that you may/not have seen, to express the
need (and perhaps you could contribute more?). Which one(s) make sense
to you; for visual reasons, 'speed', cognition, ... ?
(a) basic linear presentation:
resource = "Oil"
time = 1
crude = 2
residue = 3
my_list = "long"
(b) using explicit tuples:
( resource, time, crude, residue, my_list ) = ( "Oil", 1, 2, 3, "long" )
(c) linear and indented tuples:
(
resource,
time,
crude,
residue,
my_list
) = (
"Oil",
1,
2,
3,
"long"
)
(d) linear and differently-indented tuples:
(
resource,
time,
crude,
residue,
my_list
) = (
"Oil",
1,
2,
3,
"long"
)
(e) implicit tuples:
resource, time, crude, residue, my_list = "Oil", 1, 2, 3, "long"
NB a multiple-line expression is not possible using an 'implicit'
format! (unless one uses unattractive/distracting back-slashes)
(f) the space-saver:
resource = "Oil"; time = 1; crude = 2; residue = 3; my_list = "long"
Perhaps your editor/IDE prefers or frustrates one/other of the choices?
Perhaps your formatter/linter grinds you into submission?
Perhaps you have different approaches depending upon the number of
objects in the 'list' and the proximity of column-79, or by nature of
the application?
--
--
Regards,
=dn
More information about the Python-list
mailing list