0

在 pyyaml 文档中查看此代码,为什么"Dice(%s,%s)" % self有效?有两个%s但只有一个 var self

>>> class Dice(tuple):
...     def __new__(cls, a, b):
...         return tuple.__new__(cls, [a, b])
...     def __repr__(self):
...         return "Dice(%s,%s)" % self

>>> print Dice(3,6)
Dice(3,6)

http://pyyaml.org/wiki/PyYAMLDocumentation#Constructorsrepresentersresolvers

4

1 回答 1

0

Dice是 的派生类tupletuple长度为 2。

例如,您可以看到

>>> print yaml.dump(Dice(3,6))

!!python/object/new:__main__.Dice
- !!python/tuple [3, 6]

因此,在您的上下文中,它运行良好,它确实是一个长度为 2 的元组,因此可以解析使用的字符串。

于 2016-02-29T23:09:49.707 回答