Python Classes: Defining Custom Objects and Inheritance
In Python, we have the ability to define our own classes to create custom objects. These objects are instances of a class, and a class represents the type of an object.
To define a class, we use the following syntax:
1 | class ClassName: |
For example, let’s define a Dog
class:
1 | class Dog: |
Inside a class, we can define methods, which are functions associated with the class. For example, let’s add a bark()
method to the Dog
class:
1 | class Dog: |
In this case, self
is the argument that refers to the current object instance and must be included in the method definition.
To create an instance of a class (an object), we use the following syntax:
1 | roger = Dog() |
Now, roger
is an object of type Dog
. We can check the object’s type using the type()
function:
1 | print(type(roger)) # <class '__main__.Dog'> |
In addition to regular methods, there is a special method called __init__()
, which is known as the constructor. This method allows us to initialize properties when creating a new object. For example:
1 | class Dog: |
We can create a Dog
object and access its properties and methods like this:
1 | roger = Dog('Roger', 8) |
One important feature of classes is inheritance. We can create a parent class, such as an Animal
class, with its own methods. Then, other classes, like the Dog
class, can inherit these methods:
1 | class Animal: |
Now, objects of the Dog
class will have both the bark()
method from the Dog
class and the walk()
method inherited from the Animal
class:
1 | roger = Dog() |
By defining classes, we can create custom objects with their own properties and behaviors, and even reuse methods from other classes through inheritance.
tags: [“object-oriented programming”, “Python classes”, “inheritance”]