Python and Object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects“, which can contain data and code. The data is in the form of fields (often known as attributes or properties), and the code is in the form of procedures (often known as methods).(Wikipedia)
Summary of OOP concepts
I asked ChatGPT to help summarize the concepts of the OOP page from geeksforgeeks.org – [Archived.]
Here is a summary of the Python OOPs concepts presented on the GeeksforGeeks webpage:
- Class: A class is a blueprint for creating objects, which defines a set of attributes and methods that the objects of that class will have.
- Object: An object is an instance of a class. It has its own set of attributes and methods, and can interact with other objects and classes.
- Encapsulation: Encapsulation is the idea of bundling data and methods together, and hiding the implementation details from the outside world. In Python, this is often achieved using private and protected attributes and methods.
- Inheritance: Inheritance is the idea of creating a new class that is a modified version of an existing class. The new class, called a subclass, inherits all the attributes and methods of the original class, called the superclass, and can add its own attributes and methods.
- Polymorphism: Polymorphism is the idea that objects of different classes can be treated as if they are of the same class. This allows for greater flexibility in designing code, as different objects can be used interchangeably.
- Abstraction: Abstraction is the process of identifying the essential characteristics of an object, and ignoring the irrelevant details. In Python, this is often achieved using abstract classes and interfaces.
The GeeksforGeeks webpage also provides examples of each of these concepts in Python, as well as a brief discussion of some advanced OOPs concepts, such as multiple inheritance and method resolution order.
Attributes and examples
I further asked to clarify the concept of “attribute” which is summarized below.
In Python, an attribute is a value that is associated with an object. An attribute can be thought of as a variable that belongs to an object, and can be accessed using the dot notation.
For example, let’s say we have a class called Person
, which has the attributes name
and age
. We can create an instance of the Person
class, and set the name
and age
attributes for that instance as follows: (Shown as copiable text as well as image imprint for colored keyword clarity).
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
person1 = Person("Alice", 25)
person2 = Person("Bob", 30)
print(person1.name) # Output: "Alice"
print(person2.age) # Output: 30
n the example above, we create two instances of the Person
class, person1
and person2
. For person1
, we set the name
attribute to “Alice” and the age
attribute to 25. For person2
, we set the name
attribute to “Bob” and the age
attribute to 30. We can then access the values of these attributes using the dot notation (person1.name
and person2.age
).
Attributes can be any type of value, including other objects, functions, or data structures. They can also be modified at any time by assigning a new value to them. For example, we could modify the age
attribute of person1
as follows: (text can be copied. Image is for clarity)
person1.age = 26
print(person1.age) # Output: 26
This changes the value of the age
attribute for the person1
instance from 25 to 26.
More examples
For examples I found those on programiz.com [Archived] easier to understand, and also contains an embedded video.
Python Object Oriented Programming (OOP) – For Beginners
Beginners may find the following video useful: