0

pyyaml doc でこのコードを参照してください。なぜ"Dice(%s,%s)" % self機能するのですか? varは 2 つ%sしかあり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 に答える