Classes and Objects
I LOVE DOING THIS :)
All the code written in Python is implemented using a special construct called Classes
What is a Class
Basically a code template for creating Objects
Objects have member variables and have behaviour associated with them
Attributes
The above example has a class, but within the class there are no functionalities so it's of no use. Functionalities are defined by setting Attributes, which perform certain functions or act like some containers for storing data - These functions are called Methods
We can even assign the class to a variable - object instantiation
We can then access the attributes that are present inside the class using the dot
.
operator
Methods
When we define methods, we'll have to always provide the first argument to the method with a self
keyword
Instance attributes in python and the init method
We can also provide the values
for the attributes at runtime
This is done by defining the attributes inside the init method
Now we can directly define separate attribute values for separate objects
Inheritance
Inheritance is implemented, the methods and attributes that were defined in the base class will also be present in the inherited class
An object is based on another object, we can derive the base class as the following
Here's a simple example to master Inheritance Concept, whic uses Cars as the base class and Type as the inherited class :)
Output
Last updated