Python - is there a "don't care" symbol for tuple assignments?
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
_
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
!-).
Almost there:
var, _, value = "VAR=value".partition('=')
_
is conventionally considered a don't-care variable.
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.
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')
>>>
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 sayvar, _, 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 sayvar, sep, value
you document at the same time. This isn't very important forstr.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)
.
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).
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
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('=')
There is also the option of using indexes:
a, b = my_list[0, 2]
精彩评论