Python – is there a "don't care" symbol for tuple assignments?

Question:

Given a string “VAR=value” I want to split it (only) at the first ‘=’ sign (< value > may contain more ‘=’ signs), something like this:

var, sep, value = "VAR=value".partition('=')

Is there a way to NOT declare a variable ‘sep’? Like this (just made up the syntax):

var, -, value = "VAR=value".partition('=')

Just for completeness, I’m targetting Python v 2.6

Answers:

Almost there:

var, _, value = "VAR=value".partition('=')

_ is conventionally considered a don’t-care variable.

Answered By: SilentGhost

There isn’t anything official in the language for that; you can just use any throw-away variable. As far as standards go, I’ve seen underscores used occasionally in Python and other languages. The only issue there is that underscore is used as an alias for gettext when localizing. But if you aren’t doing localization, or aren’t using the global-binding for it, then underscore should work fine.

Answered By: DNS

Really strange question, because you can do just:

var, _, value = s.partition(sep)

and don’t care about _ variable, but _ is just a name as sep, as var or value. By the way use str.split

>>> var, value = "VAR=value".split('=')
>>> var, value
('VAR', 'value')
>>> 
Answered By: mg.

Why don’t you use 'VAR=value'.split('=') instead? That disregards the delimiter.

EDIT (to accommodate Cristi’s example in the comment):

From Diving into Python:

Tip: anystring.split(delimiter, 1) is a
useful technique when you want to
search a string for a substring and
then work with everything before the
substring (which ends up in the first
element of the returned list) and
everything after it (which ends up in
the second element).

Answered By: froadie
  • Python doesn’t have syntax to avoid assignment in unpacking and such.

  • As others have mentioned, there is a convention of using _ for variables you don’t care about. This is fairly broadly used and understood, but personally I think it is underused. If you say var, _, value = "VAR=value".partition('='), you have to know what’s going on to know what the _ was and why you didn’t care about it when you read the code. If you say var, sep, value you document at the same time. This isn’t very important for str.partition, but I’ve seen _, _, name, _, city, _ = some_weird_function() before and found it less useful than if everything was unpacked to useful names.

  • You could technically use str.split here if you wanted to. var, value = "foo=bar=baz".split("=", 1).

Answered By: Mike Graham

_ is indeed a very popular choice for “a name which doesn’t matter” — it’s a legal name, visually unobtrusive, etc. However sometimes these very qualities can hinder you. For example, the GNU gettext module for I18N and L10N, which is part of Python’s standard library, idiomatically uses _ very differently, with idioms such as…:

_ = gettext.gettext
# ...
print _('This is a translatable string.')

to mark and translate all the literal-string messages in the code (also exploiting the relative visual unobtrusiveness of _('...'). Obviously any code using this module and idiom shouldn’t also be using _ to mean something completely different (“a don’t care name”).

So a second useful alternative can be to devote the name unused to indicate such “don’t care” situations in a visually more explicit way. Google’s python style guide recommends using either _ or a prefix of unused_ — the latter can be a bit verbose but tends to be very clear, e.g.:

name, unused_surname, salutation = person_data
print "Hello, %s %s!" % (salutation, name)

makes crystal-clear that person_data is a three-item sequence (probably a tuple) and the item you’re skipping (and not using at all) is the surname (because you want to print a friendly message like “Hello, Mr Alex!” or “Hello, Miss Piggy!” ;-). (pylint and similar tools can warn you if you have unused variables named otherwise than _ or unused_..., and of course also warn you if you ever do use a variable named unused_something!-).

Answered By: Alex Martelli

The _ is commonly used as a name for you dont care * For example, in a tuple containing the name, surname and nickname in a moment in which we are interested only in name and surname, could use _ to indicate that the name is not important at this point:

data = ('John', 'Mate', 'Little John')
name, surname, _ = data
Answered By: ovrwngtvity

As others have said, underscore (_) is the standard. But if underscore is being used for translations, I think double underscore is the best alternative.

var, __, value = "VAR=value".partition('=')

Better than these:

var, unused, value = "VAR=value".partition('=')

var, unused_del, value = "VAR=value".partition('=')

var, _del, value = "VAR=value".partition('=')

Answered By: Rick Mohr

There is also the option of using indexes:

a, b = my_list[0, 2]
Answered By: Mateen Ulhaq
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.