class LineItem(model.Entity): description = model.NonBlank() weight = model.Quantity() price = model.Quantity() def __init__(self, description, weight, price): self.description = description self.weight = weight self.price = price def subtotal(self): return self.price * self.weight
class LineItem( model.Entity ): #<-model.Entity의 메타클래스 EntityMeta가 v6에서 데코레이터의 역할을 한다. 서브클래스에도 메타클래스가 확실히 적용되기 때문에, 간단히 Entity를 상속한다. description = model.NonBlank() weight = model.Quantity() price = model.Quantity() def __init__(self, description, weight, price): self.description = description self.weight = weight self.price = price def subtotal(self): return self.weight * self.price
class LineItem(model.Entity): """ 定制描述符的元类,有了元类的支持,集成model.Entity类即可:: >>> raisins = LineItem('Golden raisins', 10, 6.95) >>> LineItem.description.storage_name '_NonBlank#description' >>> raisins.description 'Golden raisins' >>> getattr(raisins, '_NonBlank#description') 'Golden raisins' """ description = model.NonBlank() weight = model.Quantity() price = model.Quantity() def __init__(self, description, weight, price): self.description = description self.weight = weight self.price = price def subtotal(self): return self.weight * self.price
class LineItem(model.Entity): ''' >>> raisins = LineItem('Golden raisins', 10, 6.95) >>> dir(raisins)[:3] ['_NonBlank#description', '_Quantity#price', '_Quantity#weight'] >>> LineItem.description.storage_name '_NonBlank#description' >>> raisins.description 'Golden raisins' >>> getattr(raisins, '_NonBlank#description') 'Golden raisins' ''' description = model.NonBlank() weight = model.Quantity() price = model.Quantity() def __init__(self, description, weight, price): self.description = description self.weight = weight self.price = price def subtotal(self): return self.weight * self.price