2

I am very new to python and need some help with instantiating an object. The python interpreter is giving me trouble when instantiating an object of a class I defined. There are two classes, BTNode and BST (which are stored in files bst_node.py and bst.py respectively):

# 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)

So, in the interpreter I do:

import bst as b
t1 = b.BST(8)

and I get an error which says that this constructor takes no arguments

The constructor clearly takes an argument value so what is going wrong here? How can I fix this error?

Thanks, all help is greatly appreciated!

1
  • 2
    Not your question but unless I'm mistaken you'll run into a problem with the lines like self.left is none because is compares, and will either be True or False. Use self.left = None instead. Commented Mar 18, 2013 at 20:57

3 Answers 3

5

The first issue is that you called your functions ___init___ instead of __init__. All of the 'special methods' use two underscores.

A second issue in this code is that in BTNode you redefined __init__. You can't overload functions in python. When you reclare __init__ you effectively deleted the first constructor.

A third issue is your usage of is. is is an operator that checks whether two objects are exactly the same and returns True or False. In the constructor, you have a few self.left is None is examining the value of self.left (which wasn't declared yet), and examining whether or not it is None. To set it, use = as follows:self.left = None

To fix the second and third issue you should use default argument values. For example:

def __init__(self, value, left=None, right=None, parent=None):

Sign up to request clarification or add additional context in comments.

Comments

4

In addition to the number of underscores problem, you should replace

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

with

def __init__(self, value, left=None, right=None, parent=None):
    """set the parameters to corresponding class members"""
    self.value = value
    self.left = left
    self.right = right
    self.parent = parent

Because as @Moshe points out, you can't overload functions, you should use default arguments insted.

Comments

2

Changing ___init___ to __init__ should fix it. (2 underscores vs 3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.