Python:导入后父类仍然是一个未定义的变量
我正在实现一个图,并将Route类作为Edge类的子类。我试图在Route类中添加“距离”特性,因此Route类的构造函数将覆盖Edge类的构造函数 由于它们位于不同的模块中,我在路由类中使用了“from Edge import*”;但是,程序(PyDev)仍然抛出错误,即未定义的变量Edge 以下是我的实现:Python:导入后父类仍然是一个未定义的变量,python,Python,我正在实现一个图,并将Route类作为Edge类的子类。我试图在Route类中添加“距离”特性,因此Route类的构造函数将覆盖Edge类的构造函数 由于它们位于不同的模块中,我在路由类中使用了“from Edge import*”;但是,程序(PyDev)仍然抛出错误,即未定义的变量Edge 以下是我的实现: ''' An (directed) edge holds the label of the node where the arrow is coming from and the lab
'''
An (directed) edge holds the label of the node where the arrow is coming from
and the label of the node where the arrow is going to
'''
class Edge:
'''
Constructor of the Edge class
'''
def __init__(self, fromLabel, toLabel):
self.__fromLabel = fromLabel
self.__toLabel = toLabel
'''
Get the label of the node where the arrow is coming from
@return the label of the node where the arrow is coming from
'''
def getFromLabel(self):
return self.__fromLabel
'''
Get the label of the node where the arrow is going to
@return the label of the node where the arrow is going to
'''
def getToLabel(self):
return self.__toLabel
你确定某个地方有一个
Edge.py
?请注意,模块/包名称通常都是小写的。我编写了Edge类,在我的src文件夹中有“Edge.py”模块。另一方面:你说我的模块应该使用小写字母?比如class Edge->class Edge和class Route(Edge)--->class Route(Edge)?不,比如模块名->Edge。阅读PEP8。您的代码对我来说就像普通的Python脚本一样工作正常(我不使用PyDev,所以我不熟悉它的错误消息--“Undefined variable”不是Python错误消息--或者它是否对边缘/边缘加倍感到困惑。)如果它不是PyDev的怪癖,我能想到的唯一一件事是,你从中导入的优势并不是你想象的那样。您可以注释掉第二个脚本中的所有内容,而不是从Edge import*中的,尝试import Edge
,然后print Edge
以排除存在的问题。
from Edge import *
'''
A Route is inherited from the Edge class and is an edge specialized for the
CSAirGraph class
'''
class Route(Edge):
'''
Constructor of the Route class
'''
def __init__(self, fromLabel, toLabel, distance):
Edge.__init__(self, fromLabel, toLabel)
self.__distance = distance
'''
Get the distance between two adjacent cities
@return: the distance between two adjacent cities
'''
def getDistance(self):
return self.__distance