Inheritance and Iterators
Inheritance
Inheritance
is when a class is created based on an existing class, and the new class
inherits the attributes and methods from the existing class. The new class is usually called
“child class”, and the existing class is called “parent class”. This similar to
the real world where some characteristics of the parent is passed on to the child.
Inheritance
in any object-oriented programming language should follow Liskov
substitution principle which says:
if S is a subtype of T, then objects
of type T may be replaced with objects of type S
It means
that the child class will inherit attributes, methods, and implementations from
the parent class. It’s allowed to modify and add new features, but not delete
features from the parent.
For example,
lets create a parent class student and child classes as Biggerboy and
Smallerboy.
Both child
classes inherit all the attributes from student, but each of them has a
different value for height.
Output will be simple 182 and 175.
Note: The __init__ () function is
called automatically every time the class is being used to create a new object.
Note: The child's __init__ () function
overrides the inheritance of the parent's __init__ () function.
super ():
This function will
make the child class inherit all the methods and properties from its parent.
By using the
super () function, you do not have to use the name of the parent
element, it will automatically inherit the methods and properties from its
parent.
Single
Inheritance
Single
inheritance is when the class inherits from only one class. Depending on what
to do in the child class, the child class may have different structures.
Example,
Lets have a parent class job and with an attribute person_name and a method
task. Create a child class employee inherited from the job and
override task with “its weekend”.
Output, this
guy seriously needs a break from all the stress 😉
Please
understand the code and analyze the output carefully.
Multi Inheritance
Multi
inheritance is when the class inherits from more than one parent class. This can,
to some extent, reduce redundancy, but it can also increase the complexity of
your code.
Example, I
have a parent class Dad and another parent class Mum. The child class Kid
extends both parent classes. The parent classes look like this. Some attributes
have the same value (e.g. city), but some not (e.g. eye_color).
If you want
to inherit attributes only from class Mum, then you can explicitly mention that
in __init__ of Kid.
Ok, so the
question comes. What is the default eye_color of a kid object? When it
comes to multi inheritance, the child class will first search the attribute in
its own class, if not, then search in its parent classes in depth-first,
left-right order. This is called Method Resolution Order (MRO) in
Python. MRO defines how Python searches for inherited methods.
Luckily, we
can check this order by doing Kid. __mro__. Since Kid class first visits
Dad, then it will have blue eyes by default. Besides, the kid will have both
swim and dancing “skills”.
Iterators
Basically,
an iterator is an object which is used to iterate through an iterable element.
It consist of the methods __iter__() and __next__().
Lists,
tuples, dictionaries, strings, and sets are all iterable objects.
Output is same as the iteration through for loop.
Create an
Iterator
To create an
object/class as an iterator you have to implement the methods __iter__()
and __next__() to your object.
Just as __init__
(), __iter__ () allows you to do some initializing when the object is being
created.
Output,
To prevent the iteration to go on forever, we used the StopIteration statement.
Hello Python people, for this blog we have taken help from the book Python : The Complete Reference.
If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".
Do subscribe to our daily blog update by clicking here.
Thank You!
No comments:
Post a Comment