一個Python 3學習者在這裏解釋蟒蛇拉鍊和拆包的解決方案: 的question有以下接受的答案: rr,tt = zip(*[(i*10, i*12) for i in xrange(4)])
返回兩個元。如果有人能夠分解答案並解釋Python 3在做什麼(我知道range()在Python 3中返回一個迭代器),我將不勝感激。我理解列表解析,但我對解包感到困惑(我以爲你只能使用星號表達式
for row in rows:
a, b, c = row
是好的,但 for row in rows:
alpha, beta, charlie, delta, echo, foxtrot, gamma, horseshoe, indigo, jimmy, killshot = row
是不是很好。 Python通常在逗號後面支持換行符很好,但我無法弄清楚這個語法。
我需要知道爲什麼失敗元組: class ConfigurationError(Exception):
def __init__(self, *args):
super(ConfigurationError, self).__init__(self, args)
self.args = list(args)
# Do some formatting o
我多次調用外部庫的方法在我的課是這樣的: class MyClass:
const_a = "a"
const_b = True
const_c = 1
def push(self, pushee):
with ExternalLibrary.open(self.const_a, self.const_b, self.const_c) as
假設您正在編寫一個您打算公開的API。 API中的函數quux返回元組的列表或生成器,例如yield (foo, bar)。 客戶端代碼通常會使用這樣的: for foo, bar in quux(whatever):
# do stuff with foo and bar
現在,假設將來你可能要開始與foo和bar返回baz在一起。你不想現在就把它退回去,因爲YAGNI直到另有證明