0
在pyyaml doc中查看此代码,为什么"Dice(%s,%s)" % self
有效?有两个%s
但只有一个变量self
?python%s字符串替换和“自我”?
>>> 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
因为'self'是包含两个项目的'tuple'。 – ekhumoro
啊......对。谢谢! – Shuman
ekhumoro - 听起来像你应该让这个答案,而不是评论 - 并被记入它。 –