Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

syntax

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

like image 809
Cristian Diaconescu Avatar asked Apr 30 '10 13:04

Cristian Diaconescu


People also ask

How do you do a tuple assignment?

Moreover, while performing tuple assignments we should keep in mind that the number of variables on the left-hand side and the number of values on the right-hand side should be equal. Or in other words, the number of variables on the left-hand side and the number of elements in the tuple should be equal.

What is tuple unpacking in Python?

Unpacking a tuple means splitting the tuple's elements into individual variables. For example: x, y = (1, 2) Code language: Python (python)

How do you know if a python is a tuple?

Method #1 : Using type() This inbuilt function can be used as shorthand to perform this task. It checks for the type of variable and can be employed to check tuple as well.

Can you strip a tuple in Python?

Removing individual tuple elements is not possible. There is, of course, nothing wrong with putting together another tuple with the undesired elements discarded. To explicitly remove an entire tuple, just use the del statement.


2 Answers

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

like image 137
Alex Martelli Avatar answered Sep 22 '22 23:09

Alex Martelli


Almost there:

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

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

like image 22
SilentGhost Avatar answered Sep 22 '22 23:09

SilentGhost