I'm a Perl guy and currently learning Python. If I have a list in Perl, I can assign its values (members) to explicit variables like so:
my ($a, $b, $c) = ('one', 'two', 'three');
Now $a == 'one'
, $b == 'two'
, $c == 'three'
. Very similar to Python.
If I'm not interested in e.g. the second member, I can write this in Perl:
my ($a, undef, $c) = ('one', 'two', 'three');
Now $a == 'one'
, and $c == 'three'
. There is no $b
. The 'two'
is simply discarded by Perl. This avoids inventing useless variables ($b
in this case) and polluting the namespace, which I appreciate.
Is there a similar idiom in Python?
toople = (1, 2, 3)
a, None, c = toople
gives SyntaxError: cannot assign to None
which sounds reasonable to me.
Is there a way to avoid the (useless) variable b
in Python?
Besides namespace pollution there's another issue that concerns me: readability and maintainability. When b
is defined, a potential maintainer has to search where b
is used (if at all). One solution would be a naming convention, like _unused_b
. Is that the solution?