
OOP stands for Object-Oriented Programming.
Python is an object-oriented language, allowing you to structure your code using classes and objects for better organization and reusability.
Tip: The DRY principle means you should avoid writing the same code more than once. Move repeated code into functions or classes and reuse it.
Classes and objects are the two core concepts in object-oriented programming.
A class defines what an object should look like, and an object is created based on that class. For example:
| Class | Objects |
|---|---|
| Fruit | Apple, Banana, Mango |
| Car | Volvo, Audi, Toyota |
When you create an object from a class, it inherits all the variables and functions defined inside that class.
Python is an object oriented programming language.
Almost everything in Python is an object, with its properties and methods.
A Class is like an object constructor, or a "blueprint" for creating objects.
To create a class, use the keyword class:
Create a class named MyClass, with a property named x:
class MyClass:
x = 5Now we can use the class named MyClass to create objects:
Create an object named p1, and print the value of x:
p1 = MyClass()
print(p1.x)You can delete objects by using the del keyword:
Delete the p1 object:
del p1You can create multiple objects from the same class:
Create three objects from the MyClass class:
p1 = MyClass()
p2 = MyClass()
p3 = MyClass()
print(p1.x)
print(p2.x)
print(p3.x)Note: Each object is independent and has its own copy of the class properties.
class definitions cannot be empty, but if you for some reason have a class definition with no content, put in the pass statement to avoid getting an error.
All classes have a built-in method called __init__(), which is always executed when the class is being initiated.
The __init__() method is used to assign values to object properties, or to perform operations that are necessary when the object is being created.
Create a class named Person, use the __init__() method to assign values for name and age:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("Emil", 36)
print(p1.name)
print(p1.age)Note: The __init__() method is called automatically every time the class is being used to create a new object.
Without the __init__() method, you would need to set properties manually for each object:
Create a class without __init__():
class Person:
pass
p1 = Person()
p1.name = "Tobias"
p1.age = 25
print(p1.name)
print(p1.age)Using __init__() makes it easier to create objects with initial values:
With __init__(), you can set initial values when creating the object:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("Linus", 28)
print(p1.name)
print(p1.age)You can also set default values for parameters in the __init__() method:
Set a default value for the age parameter:
class Person:
def __init__(self, name, age=18):
self.name = name
self.age = age
p1 = Person("Emil")
p2 = Person("Tobias", 25)
print(p1.name, p1.age)
print(p2.name, p2.age)The __init__() method can have as many parameters as you need:
Create a Person class with multiple parameters:
class Person:
def __init__(self, name, age, city, country):
self.name = name
self.age = age
self.city = city
self.country = country
p1 = Person("Linus", 30, "Oslo", "Norway")
print(p1.name)
print(p1.age)
print(p1.city)
print(p1.country)