简化类工作的实用程序
class-utils的Python项目详细描述
简化类工作的实用程序
用法:
pip install class-utils
属性:
< H4>混合蛋白:默认属性
fromclass_utilsimportdefault_propertyclassPoint():x=default_property('x')y=default_property('y')def__init__(self,x,y):self.x=xself.y=ypoint1=Point(1,4)print(point1.x)# => 1point1.x=3print(point1.x)# => 3
默认吸气剂
fromclass_utilsimportdefault_getterclassPoint():x=default_getter('x')y=default_getter('y')def__init__(self,x,y):self._x=xself._y=ypoint1=Point(1,4)print(point1.x)# => 1
键入的属性
fromclass_utilsimporttyped_propertyclassPerson():name=typed_property('name',str)def__init__(self,name):self.name=nameperson1=Person('Bill')print(person1.name)# => 'Bill'person1.name=123# => TypeError: name must be a <class 'str'>
< H4> DeffTrestPrimultMyxin < /H4>fromclass_utilsimportdefault_property,DefaultRepresentationMixinclassDate(DefaultRepresentationMixin):day=default_property('day')month=default_property('month')year=default_property('year')def__init__(self,day,month,year):self.day=dayself.month=monthself.year=yeardate1=Date(12,12,2012)print(date1)# => Date({'_day': 12, '_month': 12, '_year': 2012})
fromclass_utilsimporttyped_propertyclassPerson():name=typed_property('name',str)def__init__(self,name):self.name=nameperson1=Person('Bill')print(person1.name)# => 'Bill'person1.name=123# => TypeError: name must be a <class 'str'>
< H4> DeffTrestPrimultMyxin < /H4>
fromclass_utilsimportdefault_property,DefaultRepresentationMixinclassDate(DefaultRepresentationMixin):day=default_property('day')month=default_property('month')year=default_property('year')def__init__(self,day,month,year):self.day=dayself.month=monthself.year=yeardate1=Date(12,12,2012)print(date1)# => Date({'_day': 12, '_month': 12, '_year': 2012})