tags:

views:

341

answers:

6

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

+9  A: 

Almost there:

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

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

SilentGhost
Out of curiosity, what's the convention for multiple don't-cares? Just keep adding `_` characters? Or reuse the same `_` name?
Hank Gay
@Hank: you can just re-use single underscore, also if those variables come in a sequence you could just pack them back into a tuple, like this: `a, *_, e = range(5)`. In py2k `*_` can only be last parameter
SilentGhost
@SilentGhost: I think you're mistaken: Python 2.x doesn't allow this assignment syntax at all.
Ned Batchelder
@Ned: indeed you're right
SilentGhost
+4  A: 

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.

DNS
+3  A: 

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')
>>> 
mg
`str.split` is much slower and `str.partition` is designed for exactly this situation.
SilentGhost
@SilentGhost: good to know it.
mg
@Silent Guys, `str.partition` splits only at the **first** occurence of 'sep', so if I have var,_,value = "AAA=bbb=ccc", value will be assigned 'bbb=ccc' which is what I want; `str.split` splits at **each** occurence of 'sep'; there are more differences, see the docs. http://docs.python.org/release/2.5.2/lib/string-methods.html
Cristi Diaconescu
@Cristi Diaconescu: `split` has a `maxsplit` parameter.
mg
@mg good point.
Cristi Diaconescu
+2  A: 

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).

froadie
because <value> may contain more '=' characters, and I only want to split at the first one.
Cristi Diaconescu
Try `'VAR=value=full=of=equal=signs'.split('=',1)`.
Paul McGuire
+2  A: 
  • 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).

Mike Graham
+5  A: 

_ 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!-).

Alex Martelli