从不同的导入创建两个相同 类 的对象使它们看起来不同 类
Creating two objects of the same classes from different imports made them look as different classes
我在 Python 中有以下文件:
SB/__init__.py
from .interval import *
SB/interval.py
class Interval:
blablabla # Do stuff with intervals
def __eq__(self, other):
# Do comparison
def __lt__(self, other):
# Do comparison
def __le__(self, other):
# Do comparison
def __gt__(self, other):
# Do comparison
def __ge__(self, other):
# Do comparison
def interval_from_file(filename):
blablabla # Read the file, etc
result = []
for l in lines:
fields = l.split('\t')
... # Validate the fields
result.append(Interval(fields))
return result
如果我从 IPython shell 或 Jupyter 执行 import SB
并使用 SB.interval_from_file
加载一些数据,我会得到一个间隔对象列表。但是,如果我随后使用 SB.Interval
手动创建一个 Interval 对象,我就无法将该对象与列表中的任何其他对象进行比较。我得到
TypeError: '<' not supported between instances of 'Interval' and 'Interval'
知道发生了什么事吗?
edit:如果我打印对象的类型,则列表中的对象(因此从 SB.interval_from_file
开始具有类型 SB.interval.Interval
而对象在 IPython shell 和 SB.Interval
中创建的类型是 interval.Interval
。这种行为是预期的吗?
可能您的 pythonpath 包括 SB 及其父目录。
确保只包含适当的目录。
我在 Python 中有以下文件:
SB/__init__.py
from .interval import *
SB/interval.py
class Interval: blablabla # Do stuff with intervals def __eq__(self, other): # Do comparison def __lt__(self, other): # Do comparison def __le__(self, other): # Do comparison def __gt__(self, other): # Do comparison def __ge__(self, other): # Do comparison def interval_from_file(filename): blablabla # Read the file, etc result = [] for l in lines: fields = l.split('\t') ... # Validate the fields result.append(Interval(fields)) return result
如果我从 IPython shell 或 Jupyter 执行 import SB
并使用 SB.interval_from_file
加载一些数据,我会得到一个间隔对象列表。但是,如果我随后使用 SB.Interval
手动创建一个 Interval 对象,我就无法将该对象与列表中的任何其他对象进行比较。我得到
TypeError: '<' not supported between instances of 'Interval' and 'Interval'
知道发生了什么事吗?
edit:如果我打印对象的类型,则列表中的对象(因此从 SB.interval_from_file
开始具有类型 SB.interval.Interval
而对象在 IPython shell 和 SB.Interval
中创建的类型是 interval.Interval
。这种行为是预期的吗?
可能您的 pythonpath 包括 SB 及其父目录。
确保只包含适当的目录。