DEV Community

Cover image for Complete Python Object Oriented Programming Guide
Namima Islam
Namima Islam

Posted on

Complete Python Object Oriented Programming Guide

So you want to learn Python? Well, you've come to the right place! This blog will cover everything a beginner Python student or developer needs to know about Object Oriented Programming in Python.

But first, what is python?

Python is a powerful, high-level programming language that emphasizes simplicity and readability. It includes built-in data structures like lists and dictionaries, and supports dynamic typing, which makes it ideal for rapid application development, scripting, and prototyping.

What can Python do?

Python is great for building server-side applications, web scrapers, games, and command-line interfaces (CLIs). It can also read and write files, handle network requests and send responses, and connect too databases to access and update data. Whether you're building web servers, automation scripts, or creating data driven applications, Python is the perfect language for it!

One of the main features of python is OOP. OOP stands for object oriented programming, it allows developers to structure their code in a way that makes it modular and reusable. In Python, OOP revolves around objects and classes. An object is an instance of a class, which serves as a blueprint that defines its structure and behavior. Objects represent real world entities, like a book or car and uses data and functionality to perform actions. Objects store data in the form of attributes (also called properties) and methods (functions associated with an object).

OOP is all about reusing and organizing code by grouping related functionality together, making it easier to manage, maintain, and extend.

The key concepts of OOP in Python include:

  1. init() Method: This special method is called when an object is created, allowing for the initialization of attributes.

  2. Attributes: The properties or data associated with an object.

  3. Classes and Instances: A class is a blueprint for creating objects. An instance is a specific object created from a class.

  4. Instance Methods: Functions that belong to an object and operate on its data.

  5. Instance Variables: Variables that are unique to each instance of the class.

Now that we covered what the key concepts of Python are, lets' dive into how we can use them.

init():

Whenever we have a new object, we have to initialize it. To initialize we use the double underscore methods(also known as dunder method). The init() method is a convention, it is a special method in Python classes, commonly referred to as a constructor. It's called automatically when an object is created from a class. This method allows you to initialize the object's attributes with values when the object is instantiated.

Attributes:

An attribute is a variable that belongs to an object. Attributes are typically defined inside the init() method and are associated with individual instances. They represent the state of the object and can be accessed and modified throughout the object's lifecycle.

Image description

In the example above, the init method is initializing the parameters author, magazine, title, and below the init, the attributes are being defined using self.

Classes and Instances:

A class is like a blueprint for creating objects. A Python class defines the structure and behavior of its objects. Once the class is defined, you can create individual instances also called objects based on that blueprint.

An instance is also referred to as an object. An instance is a single occurrence of an object that refers to the individual objects produced from the class.

In my example below, class Article is the class, it is creating the blueprint for creating objects. Article1 is my instance of the class, it is the object that has been created from the class.

Image description

Instance Methods:

Instance methods are functions defined inside a class that operate on an instance of that class. These methods can access the instances attributes through the self parameter and modify the instance's attributes. They allow objects to perform actions and interact with their own data.

Image description

In the example above, the summary method defines our instance method because it operates on an individual instance of the Article class. It's using self to access the instances attributes and uses string interpolation to create the formatted string.

Instance Variables:

Instance variables are attributes that are unique to each instance of a class. They store data specific to that object. Instance variables are accessed using dot notation.

Image description

In the example above, our instance variable word_count is using dot notation and is tracking the word count of the article.

Conclusion,

Object Oriented Programming is a fundamental part of python. It uses objects to make python programs more dynamic and re-usable. If you follow the steps above you'll be a pro at OOP in no time!

Top comments (0)