+ 6
About using attributes of parent class
Class parent: def __init__(self, name, gender) Self.name=name Self.gender=gender Class son (parent): def __init__ (self, division,rollno) Self.division=division Self.rollno=rollno......... Is it possible to access the son's name and his gender along with division,roll no ,without rewritting the name and gender aatributes ,
7 Answers
+ 3
Copied from another web site.
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
def __init__(self, fname, lname, year):
Person.__init__(self, fname, lname)
# super().__init__(fname, lname) <- or use this instead of the line above
self.graduationyear = year
def welcome(self):
print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)
x = Student("Mike", "Olsen", 2019)
x.welcome()
+ 5
I don't know much about python but If son extends parent then theres no need to add that peice of code again in son becuase son inherits it from parent, just call parent constructor from son class and add the attributes in then because son inherits what ever parent has your object will have a name and gender
+ 3
iron_man = son(5, 7)
dictionary = vars(iron_man)
print(dictionary)
#Output: {'division': 5, 'rollno': 7}
+ 3
Does the son object have something to inherit?
+ 2
Seb TheS where is a concept of inheritance đ
+ 2
Atleast son object shouldn't inherit the name and gender attributes.
+ 1
Lzjb