python sub class initialiser
Solution 1
Of course, Student
can have its own initialiser. However, a class can only have one initialiser in Python, because that is a special method called within the constructor (the class __new__
method).
So when we say a sub class has its own initialiser, we really mean something like this:
class Worker(People):
def __init__(self, company):
self.company = company
As @IanH pointed out, you don't have to call the super class initialiser. And when you think you should call it (probably for some common initialization), you can do it like this:
class People:
def __init__(self, name):
self.name = name
class Student(People):
def __init__(self, name, school):
super(Student, self).__init__(name)
self.school = school
Solution 2
No it would look like this:
class Person(object):
def __init__(self, name, age, height, weight):
self.name = name
self.age = age
self.height = height
self.weigth = weight
class Student(Person):
def __init__(self, name, age, height, weight, school):
Person.__init__(self, name, age, heigth, weight)
self.school = school
If you didn't have Person.__init__
in the Student class, you would have to do all the things you do in the Person class in the Student class if you wanted to be able to use it like a person.
Solution 3
It doesn't have to be, no, but it really should. If the base class has any methods or properties which aren't overridden in the derived class then the initialiser from the base class should be called, otherwise those methods and properties may not work.
EDIT: let me clarify. You can define a initialiser for Student
, but that initialiser really should call Person.__init__
to make sure the base class is initialized properly.
Solution 4
Student
can have a own initialiser. In python you may or may not call the base class initialiser, but it is good practice to do so.
ladyfafa
Updated on June 04, 2022Comments
-
ladyfafa about 2 years
In python OOP, lets say, Person is a parent class with its own initialiser; then Student is a sub class of Person, before I use Student, must
Person.__init__(self)
be called first in the initialiser of Student? Plus, can I define a new initialiser in Student class?class Person(): def __init__(self):
Above is class Person with its initialiser
class Student(Person): def __init__(self): Person.__init__(self) def __init__(self, age)
What I mean is, could Student have its own initialiser? If so, must
Person.__init__(self)
be called in the Student initialiser in this case? -
ladyfafa almost 14 years@sth, btw, may I know how to edit the code into a nice format when I tried to post any piece of code here? Your code looks nice
-
IanH almost 14 yearsladyfafa: Just indent the code lines by 4 spaces (+ the spaces for the python code) and Stackoverflow will colorize it automagically.
-
sth almost 14 years@ladyfafa: For code blocks indent all the lines with for spaces, for example by selecting the code and clicking the 101010-button in the editor (Or using the Ctrl-K keyboard shortcut). For inline code use backticks, or that same button/shortcut. For more see stackoverflow.com/editing-help or the explanations shown to the right of the edit window.
-
habnabit almost 14 yearsAlso, you should be using
super
. -
ladyfafa almost 14 yearsthat could be brilliant, my concern is the so called "constructor overload", now i am on my way
-
Zimm3r almost 14 years@ Aaron Gallapher you can but I find it causes over confusion for something that is already confusing, Object Oriented Programing.
-
habnabit almost 14 years@Zimm3r, super makes things less confusing. What's so much more confusing about the method call in my answer?
-
Zimm3r almost 14 years@ Aaron I guess it is a matter of what parts of OOP you understand at the time.
-
clacke almost 14 yearsRegarding
super()
there are some caveats, read this: fuhm.net/super-harmful -
clacke almost 14 yearsThis is the right way to do it, but learn more about what
super()
really does here: fuhm.net/super-harmful . It's a good read in a more general sense also, as the point of using **kwargs and keyword parameters is useful in many situations. -
satoru almost 14 yearsThanks for sharing the link. But if I understood this essay correctly, it is evil only when you try to use multi-inheritance, right? So I think it is multi-inheritance that's harmful :)