Home > other >  Outer class attributes not passing to inner class Attributes for python inheritance
Outer class attributes not passing to inner class Attributes for python inheritance

Time:04-25

I don't really know what I have done wrong here. I get the error "student has no attribute name" when it gets to the output data function. Any ideas are greatly appreciated.

class Person: 
    def __init__(self):
       self.ID=""
       self.name=""
       self.address=""
       self.Phone_number=""
       self.email_id=""
       self.student=self.Student()
       
    def read_data(person):
        person.ID=input("please enter ID:")
        person.name=input("Please enter name:")
        person.address=input("Enter address:")
        person.Phone_number=input("Enter Phone Number:")

    class Student:
        def __init__(self):
            self.class_status=""
            self.major=""
        
        def read_data(student):
            student.class_status=input("Enter class status:")
            student.major=input("Enter student major:")
        
        def output_data(student):
            information=(student.name   " "   student.ID   " "   student.address   " "   student.Phone_number   " "   student.class_status   " "   student.major   "\n")
            print(information)
            studentFile.write(information)

def StudentDetails(): 
    person=Person()
    person.read_data()
    student=person.student
    student.read_data()
    student.output_data()

studentDetails()

CodePudding user response:

The attributes of an outer class aren't passed to an inner class. It looks like you're trying to model an inheritance relationship, which you can do by using subclassing rather than nesting classes. For example, you could do something like the following:

class Person: 
    def __init__(self):
       self.ID=""
       self.name=""
       self.address=""
       self.Phone_number=""
       self.email_id=""
       
    def read_data(person):
        person.ID=input("please enter ID:")
        person.name=input("Please enter name:")
        person.address=input("Enter address:")
        person.Phone_number=input("Enter Phone Number:")

class Student(Person):
    def __init__(self):
        super().__init__()
        self.class_status=""
        self.major=""
    
    def read_data(self):
        super().read_data()
        self.class_status=input("Enter class status:")
        self.major=input("Enter student major:")
    
    def output_data(self):
        information=(self.name   " "   self.ID   " "   \
          self.address   " "   self.Phone_number   " "   \
          self.class_status   " "   self.major   "\n")
        print(information)

def studentDetails(): 
    student = Student()
    student.read_data()
    student.output_data()

studentDetails()

If you are absolutely sure that you must use a nested class, then the relationship you're trying to describe doesn't make sense. I could see something like a student ID class being an inner class of Person to store some additional attributes, but I don't think the relationship as currently described makes much sense.

  • Related