【发布时间】:2021-05-10 11:03:23
【问题描述】:
我遇到了一个奇怪的错误,我认为这不是我的代码的问题。
这是我得到的物品:
- Boundary:表示域和范围,如
[1, 10],它有一个low属性和一个high属性,在本例中为low = 1和high=10 - Lim:表示联合集,如
[1, 10]U[20, 30],存储为self.boundaries = [Boundary([1, 10]), Boundary([20, 30])]
这就是我想要做的,
- 我在边界中定义了
__len__,这样len(Boundary([1, 10])) #=> 9
class Boundary:
def __len__(self):
return abs(self.high - self.low)
- 在 Lim 对象中,我有
self.boundaries,这是一个边界对象列表。定义了边界中的__len__,我将Lim 的__len__编码如下:
class Lim:
def __len__(self):
return sum([len(bd) for bd in self.boundaries])
以下是问题的发生方式,其组成如下:
class Boundary:
def __len__(self):
return abs(self.high - self.low)
class Lim:
def __len__(self):
return sum([len(bd) for bd in self.boundaries])
print(len(Lim([1, 10], [20, 30])))
# Traceback (most recent call last):
# File "boundary.py" in <module>
# print(len(Lim([1, 10], [20, 30])))
# File "boundary.py", in __len__
# return sum([len(bd) for bd in self.boundaries])
# File "boundary.py", in <listcomp>
# return sum([len(bd) for bd in self.boundaries])
# TypeError: 'float' object cannot be interpreted as an integer
但是有了这个组合:
class Boundary:
def __len__(self):
return abs(self.high - self.low)
class Lim:
def __len__(self):
return sum([bd.__len__() for bd in self.boundaries])
print(len(Lim([1, 10], [20, 30])))
# Traceback (most recent call last):
# File "boundary.py",in <module>
# print(len(Lim([1, 10], [20, 30])))
# TypeError: 'float' object cannot be interpreted as an integer
但是,代码最终以这种组合执行:
class Boundary:
def __len__(self):
return abs(self.high - self.low)
class Lim:
def __len__(self):
return sum([bd.__len__() for bd in self.boundaries])
print(Lim([1, 10], [20, 30]).__len__())
# 19
为什么将len() 更改为__len__() 会消除错误?如果你能提供一些帮助,我会很高兴。
【问题讨论】:
-
这里已经回答了这个问题:stackoverflow.com/a/2481433/2402281
-
@tahesse 对不起,我之前读过一些相关的帖子,但没有解决我的问题
-
很公平。您介意与我们分享您的
Lim和Boundary构造函数吗?我会为你起草一个演示。 -
发布的代码不会给出发布的输出。即使填写合理的
__init__方法,也不会给出贴出的输出。 tahesse 关于潜在错误的原因很可能是正确的,但问题中的代码没有重现错误。 -
__len__旨在检索序列和其他集合的大小 - 具有离散、整数大小的事物,通常受内存限制。尽管有这个名字,但它不适用于线段和曲线的长度,或者区间的大小,或者返回值需要为非整数的其他用例。
标签: python sum typeerror magic-methods variable-length