Python - 有没有“不在乎"的东西?元组赋值的符号? [英] Python - is there a "don't care" symbol for tuple assignments?
问题描述
给定一个字符串VAR=value",我想(仅)在 first '=' 符号(
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('=')
有没有办法不声明变量sep"?像这样(只是组成了语法):
Is there a way to NOT declare a variable 'sep'? Like this (just made up the syntax):
var, -, value = "VAR=value".partition('=')
为了完整起见,我的目标是 Python v 2.6
Just for completeness, I'm targetting Python v 2.6
推荐答案
_
对于无关紧要的名称"确实是一个非常受欢迎的选择——它是一个合法的名称,视觉上不显眼等等.但是有时这些特质会阻碍你.例如,用于 I18N 和 L10N 的 GNU gettext 模块,它是 Python 标准库的一部分, 习惯用法 _
的用法非常不同,例如...:
_
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").
因此,第二个有用的替代方法是使用名称 unused
以更直观的方式指示此类无关紧要"的情况.Google 的 python 样式指南 建议使用 _
或unused_
的前缀——后者可能有点冗长但往往非常清晰,例如:
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)
清楚地表明 person_data
是一个三项序列(可能是一个元组),您跳过(并且根本不使用)的项是姓氏(因为您想打印一条友好的信息,比如你好,亚历克斯先生!"或你好,小猪小姐!";-).(pylint
和类似的工具可以警告你,如果你有未使用的变量命名不是 _
或 unused_...
,当然也会警告你如果你确实使用了一个名为unused_something
!-)的变量.
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
!-).
这篇关于Python - 有没有“不在乎"的东西?元组赋值的符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!