0

How to add attributes to subclass?

# how how how # this code is error class Person: # parent class def __init__(self,name,age): self.name = name self.age = str(age) class Student(Person): # daughter class but i want to add attributes for her only def __new__(self, section): self.section = section stud1 = Student('samantha', 19, cpeb) print(stud1.__dict__)

8th Feb 2023, 12:50 AM
Reniel Galang
Reniel Galang - avatar
1 Réponse
+ 1
So, your subclass (Student) also should have it's own __init__(). If you want to get access to attributes that are from Person class, you need to learn about super(), that gives you access to superclass attributes and then you can set them for your subclass.
8th Feb 2023, 8:56 AM
Jan J
Jan J - avatar