Python中的一个模块和两个类

Python中的一个模块和两个类,python,python-2.7,Python,Python 2.7,我想在Python的一个模块/文件中包含两个类,比如BST和BSTNode。如何让BST导入/使用BSTNode class BST( object ): def __init__( self ): root = None def add(self, el): n = BSTNode(el) #other code here class BSTNode( object ): value=None left, right = None, None

我想在Python的一个模块/文件中包含两个类,比如
BST
BSTNode
。如何让BST导入/使用BSTNode

class BST( object ):

  def __init__( self ):
      root = None

  def add(self, el):
    n = BSTNode(el)
    #other code here

class BSTNode( object ):
  value=None
  left, right = None, None

  def __init__( self, el ):
    self.value=el

我认为你误解了你的错误


你在交互式口译员中输入这个吗?在这种情况下,您键入代码的顺序应该不会有什么不同。

我知道发生了什么。我首先创建了BSTNode类。然后,我开始在没有特定顺序的BST类上工作。因此,
BST.add
BSTNode
类之间的一些干预方法存在一些错误。我从未想过这会导致BSTNode不可见,但显然是这样

class BST( object ):

  def __init__( self ):
      root = None

  def add(self, el):
    n = BSTNode(el)
    #other code here

  #other unfinished methods with errors so that the BSTNode class is not seen

class BSTNode( object ):
  value=None
  left, right = None, None

  def __init__( self, el ):
    self.value=el

你说的“让BST提升BSTNode”是什么意思?如果它们在同一个模块中,则无需导入任何内容。这是我的想法,但当我尝试在BST内部使用BSTNode时,如在
n=BSTNode(el)
中,代码抱怨BSTNode是一个未定义的变量;秩序很重要。@Martijn Pieters我改变了秩序,它就起作用了。谢谢您介意将其重新发布为响应,以便我可以将其标记为已接受吗?您发布的代码只按照您定义的顺序工作(方法在执行时解析全局名称)。