'property'和'int'实例之间不支持'>'

时间:2019-11-16 09:08:03

标签: python spyder

我有以下代码:

class TransactedProperty:

    def __init__(self, address, propertyValue):
        self._address = address
        self._propertyValue = propertyValue

    @property
    def address(self):
        return self._address
    @property
    def propertyValue(self):
        return self._propertyValue
    @propertyValue.setter
    def propertyValue(self, newpropertyValue):
        self._propertyValue = newpropertyValue

    def __str__(self):
        return f'Property Address: {self._address} Value: ${self._propertyValue}'

我试图将propertyValue传递给另一个类。

class Transaction:
    _nextTransactionId = 1
    def __init__(self, transactionId, TransactedProperty, Buyer):
        self._transactionId = transactionId
        self._TransactedProperty = TransactedProperty
        self._Buyer = Buyer
        # self._ABSDRate = Buyer.getABSDRate
        # self._ABSDRate = ABSDRate
        Transaction._nextTransactionId += 1

    #ItemDelivery()
    @property
    def Buyer(self):
        return self._Buyer
    @property
    def TransactedProperty(self):
        return self._TransactedProperty

然后进行比较:

    def BSDPayable(self):
        newPropertyValue = 0
        toAdd =  0
        propertyValue = TransactedProperty.propertyValue
        while True:
            if propertyValue> 180000:
                toAdd = 180000 * 0.1
                newPropertyValue = propertyValue - 180000
                if newPropertyValue > 180000:
                    toAdd2 = 180000 * 0.2
                    BDS = toAdd + toAdd2
                    newPropertyValue = newPropertyValue - 180000
                    if newPropertyValue > 640000:
                        toAdd3 = 640000 * 0.3
                        BDS = toAdd + toAdd2 + toAdd3
                        newPropertyValue = newPropertyValue - 640000
                        if newPropertyValue > 0:
                            toAdd4 = newPropertyValue * 0.4
                            BDS = toAdd + toAdd2 + toAdd3 + toAdd4
                        else:
                            break
                    return BDS

然后致电:

我在TransactedProperty类下传递了属性值345678885,因此在调用它进行计算时应该传递过来。

def main():
        p1 = TransactedProperty('Oxley Road', 345678885)

错误代码: 'property'和'int'实例之间不支持'>' 不断出现,有什么建议吗?

1 个答案:

答案 0 :(得分:1)

使用您的实际代码来尝试比较属性对象:

from functools import reduce
def op(tree):
    return 0 if type(tree)!=list else tree[0]

def bin_to_n(tree):
    if op(tree)==0:
        return tree
    op_tree = tree[0]
    out = [op_tree]
    for node in tree[1:]:
        flat_node = bin_to_n(node)
        if op(node) != op_tree or op(node)=='-':
            out.append(flat_node)
        else:
            out += flat_node[1:]
    return out

def bin_to_n2(tree):
    if op(tree)==0:
        return tree

    op_tree = tree[0]
    if op_tree == '-' and op(tree[1]) == '-':
        return bin_to_n2(tree[1][1])
    out = [op_tree]
    for node in tree[1:]:
        flat_node = bin_to_n2(node)
        if op(node) != op_tree:
            out.append(flat_node)
        else:
            out += flat_node[1:]
    return out

#really invert according to demorgan's law
def bin_to_n3(tree, negate=False):
    if op(tree)==0:
        return tree

    op_tree = tree[0]

    if negate:
        if op_tree == '-':
            #double neg, skip the node
            return bin_to_n3(tree[1])

        #demorgan
        out = [ '+' if op_tree == '*' else '*' ]
        for node in tree[1:]:
            flat_node = bin_to_n3(node, True)
            #notice that since we modify the operators we have 
            #to take the operator of the resulting tree
            if op(flat_node) != op_tree:
                out.append(flat_node)
            else:
                out += flat_node[1:]
        return out

    if op_tree == '-' and op(op_tree)==0:
        #do not touch the leaf
        return tree

    #same code as above, not pun to factorize it
    out = [op_tree]
    for node in tree[1:]:
        flat_node = bin_to_n3(node)
        if op(flat_node) != op_tree:
            out.append(flat_node)
        else:
            out += flat_node[1:]
    return out

def calc(tree):
    if op(tree) == 0:
        return tree
    s = 0
    subtree = tree[1:]
    if op(tree)=='+':
        s = reduce(lambda x,y: x or calc(y), subtree, False)
    elif op(tree) == '-':
        s = not calc(subtree[0])
    else:
        s = reduce(lambda x,y: x and calc(y), subtree, True)
    return s

#adaptated from https://stackoverflow.com/questions/6881170/is-there-a-way-to-autogenerate-valid-arithmetic-expressions
def brute_check():
    import random
    random.seed(3)
    def make_L(n=3):
        def expr(depth):
            if depth==1 or random.random()<1.0/(2**depth-1): 
                return random.choice([0,1])
            if random.random()<0.25:
                return ['-', expr(depth-1)]
            return [random.choice(['+','*']), expr(depth-1), expr(depth-1)]
        return expr(n)

    for i in range(100):
        L = make_L(n=10)
        a = calc(L)
        b = calc(bin_to_n(L))
        c = calc(bin_to_n2(L))
        d = calc(bin_to_n3(L))
        if a != b:
            print('discrepancy', L,bin_to_n(L),  a, b)

        if a != c:
            print('discrepancy', L,bin_to_n2(L),  a, c)

        if a != d:
            print('discrepancy', L,bin_to_n3(L),  a, d)
brute_check()

您需要正确初始化:

propertyValue = TransactedProperty.propertyValue
propertyValue
>>> <property object at 0x7f190c1db410>