Python实例变量包含另一个类的实例,但是为什么我不能访问该变量的实例变量

时间:2019-12-12 06:23:14

标签: python object binary-search-tree nodes instance-variables

我有一个Student类,Node类和Tree类。基本上,我想将Student类的实例传递给Node类的实例变量。我试图访问包含Student类实例的Node实例变量的实例变量。为什么会出现错误?抱歉,我通常使用Java编写代码,试图理解Python。

这是我的代码:

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)

我在尝试执行node.data.age的Tree类时遇到错误

Node类的实例变量“数据”应包含Student类的实例。我为什么不能做些什么?

这是我的主要爱好:

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

2 个答案:

答案 0 :(得分:1)

希望这就是您要寻找的。 另外,在编写python代码时,请尝试遵循PEP8编码标准。

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)

答案 1 :(得分:0)

似乎从未定义过node.data.age。您是否尝试过执行node.age?

<ul>

当您传递值时,您正在将数据设置为值,但是没有多余的内容被保存。