我要替换a.py的实现用这种方式使用b.py,怎么写?

时间:2018-09-21 10:58:38

标签: python-2.7

a.py

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

b.py

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

1 个答案:

答案 0 :(得分:0)

DataTable