【发布时间】:2018-05-19 14:36:12
【问题描述】:
我使用attrs 来定义没有样板代码的简单类。装饰器自动生成一个__repr__,显示所有属性的值。我只想显示没有默认值的属性:
>>> import attr
>>> @attr.s
... class Coordinates(object):
... x = attr.ib(default=0)
... y = attr.ib(default=0)
>>> Coordinates() # wanted output: Coordinates()
Coordinates(x=0, y=0)
>>> Coordinates(x=0, y=0) # wanted output: Coordinates()
Coordinates(x=0, y=0)
>>> Coordinates(x=1) # wanted output: Coordinates(x=1)
Coordinates(x=1, y=0)
>>> Coordinates(x=1, y=1) # output OK
Coordinates(x=1, y=1)
是否有任何相当简单的方法来实现这一点?
【问题讨论】:
标签: python-attrs