2

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?

zdim
  • 53,586
  • 4
  • 45
  • 72
PerlDuck
  • 5,342
  • 3
  • 16
  • 35
  • Have you considered to reduce right-hand-side instead of unpacking a whole tuple? – rth Apr 13 '17 at 21:34
  • @rth Yes. But that's not feasable in my case. I'm writing plugins for a 3rd party tool and the function I'm supposed to write hands me a tuple with N items, but I'm only interested in some of them. – PerlDuck Apr 13 '17 at 21:46
  • If you save your tuple, you can manipulate it, like this `x=(1,2,3),A,B=x[:1]+x[2:]`, isn't it? But it isn't good, I know. numpy can do fancy thing, but only with numbers. – rth Apr 13 '17 at 21:59
  • 1
    `a, _, c = toople` - Yes, it's just `b` by another name but `_` is generally accepted to be a throwaway variable name, and you can repeat it a la `a, _, _, _, e` – ryugie Apr 13 '17 at 22:07
  • Hmm. There was a nice answer which suggested `a, _, c = (1,2,3)`. It was downvoted and deleted. But I liked it because it suggested there is no _real_ way to avoid the extra variable and that's what I wanted to know. – PerlDuck Apr 13 '17 at 22:12
  • 1
    @PerlDuck - Do note that if you're doing any internationalization or localization, `_` is often already in use – ryugie Apr 13 '17 at 22:16
  • @ryugie Ahh, very good point. My plugin's framework already makes heavy use of `_("terms to be translated")`. – PerlDuck Apr 13 '17 at 22:33

1 Answers1

2

Since you select by position either take specific elements

a, c = [ toople[i] for i in [0,2] ]

or exclude the others

a, c = [ item for i, item in enumerate(toople) if i not in [1] ] 

These use list comprehension and enumerate

A way reminiscent of Perl's undef is to use _ as a throwaway variable, but as noted in comments this conflicts with internationalization where _ may be in use. See answers in this post.

Community
  • 1
  • 1
zdim
  • 53,586
  • 4
  • 45
  • 72
  • Hey zdim! :-) I wasn't aware you're also familiar with Python. Albeit your proposal does exactly what I asked for, it seems over-complicated compared to the Perl thing. But if that is the solution, I'll consider it. My tuple only has few elements (5…15). Depending on my actual case, I'll use your solution or some naming convention (like `_no_b`). – PerlDuck Apr 13 '17 at 22:28
  • Hey :))) There are ways to 'filter' on assignment but I don't know that there's anything equivalent to the Perl's `undef` ... "placeholder" (? how do I even call that :). The _list comprehensions_ are good. With contiguous indices (ranges) the slices are better of course. – zdim Apr 13 '17 at 22:45
  • @PerlDuck I added another way which is better alined with excluding a small set of specific indices, and noted `_` with a link to a nice page about it – zdim Apr 17 '17 at 17:25