Object-oriented programming (OOP) involves the use of objects to create programs. An object represents an entity in the real world that can be distinctly identified. For example, a student, a circle, a button... An object has a unique identity, state, and behavior.
Object consists of: Data fields: state, properties, attributes; Methods: behavior, actions.
Object-oriented programming enables you to develop large-scale software and GUIs effectively. It is also easy to design, implement, test, debug, update.
The four pillars of OOP: abstraction, encapsulation, inheritance, polymorphism.
In programming languages, the class is a Data Type, a Blueprint, or a Template. The object is the working instance of the class, and one or more variables can hold a reference to an instance.
An object is an instance of a class, and you can create many instances of a class. Creating an instance of a class is referred to as instantiation. The terms object and instance are often used interchangeably. An object is an instance and an instance is an object.
<aside> 💡
In Summary:
__init__()
is the constructor method in Python, which initializes the object’s attributes with default or specified values. When a new instance is created, this __init__()
method is called.
__del__()
is the destructor method in Python, which automatically destroys the object when it no longer has any references.
Object Example: Human class with name, age, gender, city; Methods: can walk(), can sleep(), can speak(), can eat().
Ed Corner, 32, Male, New York —> This is an instance of the Human class.
# Initialize the Class
class Human:
def __init__(self, name, age, gender, city): # initializes the attributes
print("This is an instance of the Human Class")
self.name = name
self.age = age
self.gender = gender
self.city = city
def main():
# Create an instance Student
p1 = Human("Ed Corner", 32, "Male", "New York")
print(p1)
main()
Output:
This is an instance of the Human Class
<__main__.Human object at 0x10cecce90>