自动或声明性地创建repr。
Represent的Python项目详细描述
安装
$ pip install represent
自动生成
fromrepresentimportautorepr@autoreprclassRectangle:def__init__(self,name,color,width,height):self.name=nameself.color=colorself.width=widthself.height=heightrect=Rectangle('Timothy','red',15,4.5)print(rect)
Rectangle(name='Timothy', color='red', width=15, height=4.5)
声明生成
fromrepresentimportReprHelperMixinclassContrivedExample(ReprHelperMixin,object):def__init__(self,description,radians,shape,color,miles):self.description=descriptionself.degrees=radians*180/3.141592654self.shape=shapeself._color=colorself.km=1.60934*milesdef_repr_helper_(self,r):r.positional_from_attr('description')r.positional_with_value(self.degrees*3.141592654/180)r.keyword_from_attr('shape')r.keyword_from_attr('color','_color')r.keyword_with_value('miles',self.km/1.60934)ce=ContrivedExample('does something',0.345,'square','red',22)print(ce)fromIPython.lib.prettyimportpprintpprint(ce)
ContrivedExample('does something', 0.345, shape='square', color='red', miles=22.0) ContrivedExample('does something', 0.345, shape='square', color='red', miles=22.0)