恶意的Python仪式在他们不应该工作时工作

mat*_*ius -4 python lambda python-3.x

我有以下代码:

>>> pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
>>> pairs.sort(key=lambda pair: pair[1])
>>> pairs
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
Run Code Online (Sandbox Code Playgroud)

我刚刚接受了lambda函数,我得到了它们的工作原理,至少,我能理解他们恶魔黑暗魔法背后的邪恶仪式.

但即使Python代码让我疯狂,我仍然不明白为什么这段代码有效?pair没有以任何方式定义为什么存在访问索引,更糟糕的是,为什么该索引很重要(fyi pair[2]超出范围并pair[0]给出正常的有序pairs).

我们到底如何才能获得仅存在于可怕的lambda函数的不纯约束中的虚无?此外,当我们凝视它时,获取虚无的东西如何回归到我们眼中的任何空洞?

AKX*_*AKX 6

# The following incantation shall summon from the depths
# of the data abyss thirteen entities: four tuples,
# four integers, four strings and a list containing all these.
# The amalgamation of these entities, the list, shall be bound
# to the name `pairs` to further do our dark bidding without
# fleeing into nothingness.

pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]

# One of the 133,316,666 demons is also bound by the spirit of
# Tim with the "member" `sort`. Another way to call upon the spirit
# of Tim is the `sorted` name, though it will use its dark magic
# to invoke another copy of the list, and thus would waste our magics
# unnecessarily.
# The sort demon can be wrangled to not compare things by their inherent
# value, but by another incantation, a `function`. The demon will then
# invoke that incantation for each of the things it compares, and use
# that value for sorting.
# (Before Python III, there also used to be another way, a "comparison"
# function, but this way is easier, which is likely why it was banished.
# Why else be a wizard if one did not wish ease and comfort?)
# The `lambda` word of power is equivalent to the more familiar `def`
# form, as such:
#
#    lambda pair:    |  def ANONYMOUS(pair):
#      pair[1]       |    return pair[1]
#
# The similarities and differences are quite easy to spot.
# 
# And indeed, if one were to `def ANONYMOUS`, they could form this incan-
# tation as `key=ANONYMOUS` instead.
#
# And for, as we mentioned earlier, the dark things confined within the
# list bound to the name `pairs` are tuples, which further confine within
# themselves an integer and string each, all accessible to our hands by
# indexing by zero (which is the Only True Way) by the bracket sigil [x],
# it only makes sense for the function to do just that to pass the value
# to the sorting demon that way.
# An illustration of this "indexing" for the fledgling wizard:
#
# [             | pairs
#    (          | pairs[0]
#        1,     | pairs[0][0]
#       'one'   | pairs[0][1]
#    ),
#    (          | pairs[1]
#       2,      | pairs[1][0]
#       'two'   | pairs[1][1]
#    )
# ]
#

pairs.sort(key=lambda pair: pair[1])

# And so, the mage may laugh and enjoy themselves, watching the demon
# futilely sort his items, and eventually we may gaze upon the fruits
# of his effort. Not the demon's, of course, for it is but a tool for the
# great wizard.

pairs
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
Run Code Online (Sandbox Code Playgroud)

  • 在Python 3中,`cmp`参数被放逐到了Hades的9个领域的最内圈.在它的位置,甚至更恶魔般的[`functools.cmp_to_key()`incantation](https://docs.python.org/3/library/functools.html#functools.cmp_to_key)出现了令人困惑的巫师更加困惑. (2认同)