class quan(object):
__couter=0
def __init__(self):
cls=self.__class__
prefix=cls.__name__
index=cls.__couter
self.storage_name='_{}#{}'.format(prefix,index)
cls.__couter+=1
def __get__(self, instance, owner):
return getattr(instance,self.storage_name)
def __set__(self, instance, value):
if value>0:
setattr(instance,self.storage_name,value)
else:
raise ValueError('value must be > 0')
class LineItem(object):
w=quan()
p=quan()
def __init__(self,d,w,p):
self.d=d
self.w=w
self.p=p
def sub(self):
return self.w * self.p
r=LineItem('aa',10,10)
print r.w,r.p
class quan(object):
__couter=0
def __init__(self):
cls=self.__class__
prefix=cls.__name__
index=cls.__couter
self.storage_name='_{}#{}'.format(prefix,index)
cls.__couter+=1
def __get__(self, instance, owner):
return getattr(instance,self.storage_name)
def __set__(self, instance, value):
if value>0:
setattr(instance,self.storage_name,value)
else:
raise ValueError('value must be > 0')
class LineItem(object):
def __init__(self,d,w,p):
self.d=d
self.w=w
self.p=p
def sub(self):
return self.w * self.p
@quan
def w(self,v):
self._w=v
@quan
def p(self,v):
self._p=v
答案 0 :(得分:0)
DataTable