Understanding Classes and Objects in Python: An Introduction to OOP
Imagine you have a box of different toys — cars, dolls, and blocks. Each toy has its own features and actions. That’s similar to how Object-Oriented Programming (OOP) works.
In OOP, we think of the software as a collection of objects, like those toys. Each object represent something specific like car, person or dog. They all have their unique characteristics called attributes and unique functions called methods. For example: If a car is an object, then it has attributes like color, brand, seats, wheel, engine, speed, etc. But it doesn’t only have attributes, but also methods like drive, stop, honk, etc.
But here is what is interesting. You can take those old toys and make a lot of new toys using old blueprint. Such objects created from blueprint of original objects are called instances, which inherits all attributes and methods of the original objects.
This is useful when we want to create a lot of new objects with their unique attribute and methods, but they all follow the same blueprint of original objects.
In nutshell, OOP is like playing with different toys that have their own features (attributes) and do cool things (methods). We can create new toys based on old ones and give them unique features and actions.
Defining Class And Objects In Python
Creating a new class is like creating your own unique custom data type using built-in data like strings, list, integers, etc. While functions are reusable, OOP takes it to the whole new level by giving your data type necessary attributes and functions.
Let’s take a look at the example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def say_hello(self):
print(f"Hello, my name is {self.name} and I am {self.age} years old.")
# Create an instance of the Person class
person1 = Person("Alice", 25)
# Access the attributes of the person1 object
print(person1.name) # Output: Alice
print(person1.age) # Output: 25
# Call the say_hello method of the person1 object
person1.say_hello() # Output: Hello, my name is Alice and I am 25 years old.
Here, a class named Person is defined. After that, a special function init called constructor is called taking inputs i.e. self, name and age which gets initialized without calling when a new instance is defined from the class. self is the term used to represent the object itself, and it is not compulsory to use this term like this keyword in JavaScript. But it is conventional to use the term self to represent an object by itself.
Inside init function, attributes like name and age are initialized with the value of arguments. After that, a function named say_hello() which makes the person object to introduce itself with given value of name and age. An instance of Person named person1 is created with inputs name (Alice) and age (25). Later, the attributes of person1 are accessed and person1 introduces itself using the method say_hello().
Let’s take another interesting example:
Here, a class named Pokemon is created taking three inputs i.e. name, type and health. Remember, self represents the object itself and is taken as input to access other attributes and methods inside a function.
pikachu = Pokemon(pikachu,electric,250) # creating instance of object
print(pikachu.attack()) # result: Electric attack!!. Zhoop!!!
print(pikachu.dodge()) # result: Pikachu Dodge!
print(pikachu.evolev()) # result: Evvolving to Raichu!!!!
print(pikachu.Type) # result: electric
This concludes the fundamental understanding of OOP in Python.