+ 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 ,

15th Aug 2019, 12:21 PM
Sagar Hiremath
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()
15th Aug 2019, 5:57 PM
rodwynnejones
rodwynnejones - avatar
+ 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
15th Aug 2019, 1:49 PM
D_Stark
D_Stark - avatar
+ 3
iron_man = son(5, 7) dictionary = vars(iron_man) print(dictionary) #Output: {'division': 5, 'rollno': 7}
15th Aug 2019, 1:21 PM
Seb TheS
Seb TheS - avatar
+ 3
Does the son object have something to inherit?
15th Aug 2019, 1:53 PM
Seb TheS
Seb TheS - avatar
+ 2
Seb TheS where is a concept of inheritance 😅
15th Aug 2019, 1:29 PM
Sagar Hiremath
+ 2
Atleast son object shouldn't inherit the name and gender attributes.
15th Aug 2019, 1:55 PM
Seb TheS
Seb TheS - avatar
+ 1
Lzjb
15th Aug 2019, 6:06 PM
farooq