【发布时间】:2018-02-03 21:20:05
【问题描述】:
Tl;dr 在我的“变量”类中,在创建一个新实例时,我让它执行一个名为 check() 的函数,以查看其内部是否有任何相同类型的变量,然后它可以简化它,但不知何故,如果__new__ 函数的那部分中使用的数据不是False 并且数据永远不能是True,那么我会得到一个布尔错误,但之前的代码片段在哪里出现错误事件不会触发
所以,我一直在制作这个 python 类,它代表一个你不知道其值的变量,并且正在努力使 atau 类尽可能简化它自己的值,并得到了一些非常奇怪的结果;
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
【问题讨论】:
-
你为什么要把
check设为全局(我强烈怀疑这是你的问题)?另外,你为什么在你的check()方法中返回insert()的结果,它总是返回None? -
哦。我认为
.insert(0, arg)与.append(arg)正好相反。 -
我现在看到了我的错误。
-
我应该直接插入列表本身。
-
删除
global check,我想你会发现你的代码有效。我相信您的调用eval(new)会调用variable.__new__(),这会在您检查之后将您的全局变量check设置为False。
标签: python python-3.x class boolean typeerror