Search code examples
pythonobjectbinary-search-treenodesinstance-variables

Python Instance Variable contains an instance of another class but why can't I access the instance variable of that variable


I have a Student class, Node class, and Tree class. Basically, I want to pass an instance of the Student class to an instance variable of the Node class. I'm trying to access the instance variable of the Node's instance variable that contain that instance of the Student class. Why am I getting an error? Sorry I'm usually code in Java, trying to understand Python.

Here is my code:

class Node(object):

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

    def __repr__(self):
        return str(self.data.__repr__)

    def __str__(self):
        return str(self.data.__str__)

class Tree(object):
    def __init__(self):
        self.root = None

    def addNode(self, node, value):
        if node is None:
            self.root = value
        else:
            if value.data.age<node.data.age:
                if node.left is None:
                    node.left = value
                else:
                    self.addNode(node.left, value)

            else: 
                if node.right is None:
                    node.right = value
                else:
                    self.addNode(node.right, value)

    def print_in_order(self, node):
        if node is not None:
            self.print_in_order(node.left)
            print(node)
            self.print_in_order(node.right)

class Student(object):
    def __init__(self, fname, lname, major, age):
        self.fname = fname
        self.lname = lname
        self.major = major
        self.age = age

    def __repr__(self):
        return "%s %s" % (self.fname, self.lname)

    def __str__(self):
        return "Student name: %s %s. Major's in %s and is %s years old" % (self.fname, self.lname, self.major, self.age)

#make student information
student1 = Student("Richard", "Nono", "Electrical Engineer", 22)
student2 = Student("Happy", "Tail", "Psychology", 19)
student3 = Student("Honda", "Make", "Mechanical Engineer", 21)
student4 = Student("John", "Ladgo", "Computer Science", 20)
student5 = Student("Remember", "Assignment", "Music", 23)

#make nodes from students
node1 = Node(student1)
node2 = Node(student2)
node3 = Node(student3)
node4 = Node(student4)
node5 = Node(student5)

#make tree and populate it witht he nodes
bts = Tree()
bts.addNode(bts.root, node1)
bts.addNode(bts.root, node2)
bts.addNode(bts.root, node3)
bts.addNode(bts.root, node4)
bts.addNode(bts.root, node5)

#print the tree
bts.print_in_order(bts.root)

I get an error in my Tree class where I try to do node.data.age

The instance variable "data" of the Node class should contain an instance of the Student class. Why can't I do something that?

Here is my main:

def main():
    #make student information
    student1 = Student("Richard", "Nono", "Electrical Engineer", 22)
    student2 = Student("Happy", "Tail", "Psychology", 19)
    student3 = Student("Honda", "Make", "Mechanical Engineer", 21)
    student4 = Student("John", "Ladgo", "Computer Science", 20)
    student5 = Student("Remember", "Assignment", "Music", 23)

    #make nodes from students
    node1 = Node(student1)
    node2 = Node(student2)
    node3 = Node(student3)
    node4 = Node(student4)
    node5 = Node(student5)

    #make tree and populate it witht he nodes
    bts = Tree()
    bts.addNode(bts.root, node1)
    bts.addNode(bts.root, node2)
    bts.addNode(bts.root, node3)
    bts.addNode(bts.root, node4)
    bts.addNode(bts.root, node5)

    #print the tree
    bts.printInorder(bts.root)

main()

Solution

  • Hope This is what you are looking for. Also please try to follow PEP8 coding standard while writing python code.

    class Node(object):
    
        def __init__(self, value):
            self.left = None
            self.right = None
            self.data = value
    
        def __repr__(self):
            return self.data.__repr__()
    
        def __str__(self):
            return self.data.__str__()
    
    
    class Tree(object):
    
        def __init__(self):
            self.root = None
    
        def add_node(self, node, value):
            if node is None:
                self.root = value
            else:
                if value.data.age < node.data.age:
                    if node.left is None:
                        node.left = value
                    else:
                        self.add_node(node.left, value)
    
                else:
                    if node.right is None:
                        node.right = value
                    else:
                        self.add_node(node.right, value)
    
        def print_in_order(self, node):
    
            if node is not None:
                self.print_in_order(node.left)
                print(node)
                self.print_in_order(node.right)
    
    
    class Student(object):
    
        def __init__(self, fname, lname, major, age):
            self.fname = fname
            self.lname = lname
            self.major = major
            self.age = age
    
        def __repr__(self):
            return "%s %s" % (self.fname, self.lname)
    
        def __str__(self):
            return "Student name: %s %s. Major's in %s and is %s years old" % (self.fname, self.lname, self.major, self.age)
    
    
    student1 = Student("Richard", "Nono", "Electrical Engineer", 22)
    student2 = Student("Happy", "Tail", "Psychology", 19)
    student3 = Student("Honda", "Make", "Mechanical Engineer", 21)
    student4 = Student("John", "Ladgo", "Computer Science", 20)
    student5 = Student("Remember", "Assignment", "Music", 23)
    
    node1 = Node(student1)
    node2 = Node(student2)
    node3 = Node(student3)
    node4 = Node(student4)
    node5 = Node(student5)
    
    
    bts = Tree()
    bts.add_node(bts.root, node1)
    bts.add_node(bts.root, node2)
    bts.add_node(bts.root, node3)
    bts.add_node(bts.root, node4)
    bts.add_node(bts.root, node5)
    
    
    bts.print_in_order(bts.root)