代码的一部分错误,不应该在第一个地方触发

时间:2017-08-25 19:00:11

标签: python python-3.x class boolean typeerror

Tl;博士在我的"变量" class,在创建一个新实例时,我让它做一个名为check()的函数,看看是否有任何相同类型的变量本身,所以它可以简化它,但不知怎的,我得到一个bool错误的东西只有在__new__功能的那一部分中使用的数据不是False并且数据永远不会是True时才应该出现,但是错误发生之前的代码片段不是&# 39; t触发

所以,我一直在制作这个代表变量的python类,你不知道它的价值,并且正在努力使这个类更加简单 - 如果有的话,简化它自己的价值可能,并得到一些非常奇怪的结果;

class variable:
    def __new__(cls, name = "x", value = 1, power = 1):
        self = object.__new__(cls)
        self.name = name
        if value == 0:
            return 0
        if name == "" and value == 1:
            return 1
        if name == "" and not isinstance(power, variable):
            return value**power
        else:
            self.power = power
            self.value = value
        if not isinstance(self.value, variable):
            return self
        global check
        check = self.value.check(name)
        print(repr(check))
        if check == False:
            return self
        new = ""
        for x in check[1:-1]:
            new += "variable(x.name, "
        if isinstance(check[-1].value, str):
            new += "'" + check[-1].value + "'"
        else:
            new += repr(check[-1].value)
        for x in list(reversed(check[1:-1])):
            new += repr(x.power) + ")"
        print("'")
        print(new)
        print(self)
        print("'")
        if new == None:
            new = 1
        return variable(self.name, eval(new), self.power + check[-1].power)

    def check(self, name):
        if self.name == name:
            return [self,]
        if isinstance(self.value, variable):
            if not self.value.check(name) == False:
                return self.value.check(name).insert(0, self)
        return False
    def __str__(self):
        return (not self.value == 1)*str(self.value) + (not self.power == 1)*"(" + str(self.name) + (not self.power == 1)*(")^" + str(self.power))
    def __repr__(self):
        if isinstance(self.value, str):
            return "variable('" + self.name + "', '" + self.value + "', " + repr(self.power) + ")"
        return "variable('" + self.name + "', " + repr(self.value) + ", " + repr(self.power) + ")"
    def __add__(self, other):
        if isinstance(other, variable) and (self.name == other.name and self.power == other.power):          
            return variable(other.name, self.value + other.value, other.power)
        else:
            return NotImplemented
    def __sub__(self, other):
        if isinstance(other, variable) and (self.name == other.name and self.power == other.power):          
            return variable(other.name, self.value - other.value, other.power)
        else:
            return NotImplemented
    def __mul__(self, other):
        if isinstance(other, variable) and self.name == other.name:
            return variable(self.name, self.value*other.value, self.power+other.power)
        elif isinstance(other, int) or isinstance(other, float) or isinstance(other, variable):
            return variable(self.name, self.value*other, self.power)
        else:
            return NotImplemented
    def __truediv__(self, other):
        if isinstance(other, variable) and self.name == other.name:
            return variable(self.name, self.value/other.value, self.power-other.power)
        elif isinstance(other, int) or isinstance(other, float) or isinstance(other, variable):
            return variable(self.name, self.value/other, self.power)
        else:
            return NotImplemented
    def __pow__(self, other):
        if isinstance(other, int) or isinstance(other, float) or isinstance(other, variable):
            return variable(self.name, self.value**other, self.power*other)
        else:
            return NotImplemented
    __radd__ = __add__
    __rsub__ = __sub__
    __rmul__ = __mul__
    def __rtruediv__(self, other):
        return other**(self**-1)  
    def __rpow__(self, other):
        if isinstance(other, variable):
            return variable(other.name, other.value**self, other.power*self)
        elif isinstance(other, int) or isinstance(other, float):
            return variable("", other, self)
        else:
            return NotImplemented

我进入了;

>>> variable("x", variable("y", variable("z", variable("y", variable("y", variable("z", variable("x")))))))

结果;

False
False
[variable('y', variable('z', variable('x', 1, 1), 1), 1)]
'
variable('z', variable('x', 1, 1), 1)
xzyy
'
False
Traceback (most recent call last):
  File "<pyshell#34>", line 1, in <module>
    variable("x", variable("y", variable("z", variable("y", variable("y", variable("z", variable("x")))))))
  File "C:\Users\Alexander van Helm\Desktop\Python\herleiden.py", line 62, in __new__
    return variable(self.name, eval(new), self.power + check[-1].power)
TypeError: 'bool' object is not subscriptable

2 个答案:

答案 0 :(得分:0)

返回语句check[-1].power中的

return variable(self.name, eval(new), self.power + check[-1].power)是问题所在。

check()方法返回bool。这意味着它只能是FalseTrue两件事之一。您无法在check[-1]类型上执行此bool之类的操作。这就是TypeError TypeError: 'bool' object is not subscriptable试图告诉你

答案 1 :(得分:0)

删除global check,我认为您会发现您的代码有效。我相信您的来电eval(new)会调用variable.__new__(),然后会在您检查后将全局变量check设置为False

(评论转回答案)