저는 파이썬에 매우 익숙하며 객체를 인스턴스화하는 데 도움이 필요합니다. Python 인터프리터는 내가 정의한 클래스의 객체를 인스턴스화 할 때 문제가 발생합니다.파이썬 객체 인스턴스화
# file: bst_node.py
class BTNode:
"""a binary search tree node implementation"""
def ___init___(self, value):
self.value = value
self.left is None
self.right is None
self.parent is None
def ___init___(self, value, left, right, parent):
"""set the parameters to corresponding class members"""
self.value = value
self.left = left
self.right = right
self.parent = parent
def is_leaf(self):
"""check whether this node is a leaf"""
if self.left.value is None and self.right.value is None:
return True
return False
# file: bst.py
from bst_node import *
class BST:
"""a binary search tree implementation"""
def ___init___(self, value):
self.root = BTNode(value)
def insert(self, curRoot, newValue):
if curRoot.is_leaf():
if newValue < curRoot.value:
newNode = BTNode(newValue, None, None, curRoot)
curRoot.left = newNode
else:
newNode = BTNode(newValue, None, None, curRoot)
curRoot.right = newNode
else:
if newValue < curRoot.value:
self.insert(curRoot.left, newValue)
else:
self.insert(curRoot.right, newValue)
인터프리터에서 내가 할, 그래서 : (각각 파일 bst_node.py
및 bst.py
에 저장됩니다) 두 개의 클래스, BTNode
및 BST
이 있습니다
import bst as b
t1 = b.BST(8)
및 I 그 말을하는 오류가 발생합니다. constructor takes no arguments
생성자는 분명히 인수 value
을 취해서 여기서 무엇이 잘못 될까요? 이 오류를 수정하려면 어떻게해야합니까?
감사합니다. 모든 도움을 주시면 대단히 감사하겠습니다.
당신의 질문은 아니지만,'is'가 비교 되었기 때문에'self.left is none'과 같은 줄에 문제가 생기지 않는다면'True' 또는'False'가 될 것입니다. 대신에'self.left = None'을 사용하십시오. – askewchan