Python: Classes, Objects, Instances and Attributes?

As a developer, you need to be familiar with the terms associated with the various programming languages. Every Programming language has its own syntax, terms and keywords. Generally though, you will always come across the terms classes, objects, instances and attributes in a number of programming languages. In Python though, what do these terms mean?

Classes

Python is an Object Oriented Programming language meaning everything in Python is an “Object” with its individual properties and methods. A class is like an Object constructor or a blueprint for creating Objects. It describes the contents of the Objects that belong to it and also describes the various data fields or instance variables as well as defines the methods of the Object.

How to create a class?

Lets take a look at this example. Lets create a class named FirstClass, with a property named y:

class FirstClass:

y = 5

The Pass Statement

When defining classes, they cannot be empty but if for some reason, you want to define a class with no content, you have to make use of a pass statement to avoid getting an error. For example:

class FirstClass:

pass

This code will run without any error because of the pass statement used even without no content.

The built-in __init__ function()

When instantiating classes, you make use of the “__init__() function” to assign values to object properties or other operations that are required to be done when the object is being created. It is very important when instantiating classes.

The Self Parameter

The self parameter is a reference to the current instance of a class and it is used to access the various variables or instances that belong to the class. Note though that it is not compulsory to name it “self”, you can name it whatever you like, but it has to be the first parameter of any function declared in the class.

Lets now take a look at this example that sums up all we have learned so far. Let us create a class named Man, then use the init function to assign values for name and occupation.

class Man:

def __init__(self, name, occupation)

self.name = name

self.occupation = occupation

m1 = Man(“Ayo”, “Banker”)

print(m1.name)

print(m1.occupation)

m1 is an object of class Man. We shall now take a look at what Objects in Python are:

Objects

As mentioned earlier, everything in Python is an object. Therefore, objects are variables that contain data and functions that can be used to manipulate the data.

How to create an object?

Lets say we want to make use of the class we defined earlier which was FirstClass to create an object named “m1” and print the value of “y”.

m1 = FirstClass()

print(m1.y)

From the example above, we have successfully created an object of the class FirstClass named “m1” which will output the value of “y”.

Even after objects have been defined, they can still be deleted using the “del” keyword and their properties can also be modified and deleted.

Instances

An instance attribute is a python variable belonging to only one object. This Python variable is only accessible in the scope of this object and its defined inside the init function of the class. An example is:

class FirstClass:

def __init__(self, name, occupation)

In the illustration above, “name” and “occupation” are the instances of the function defined in class FirstClass.

Attributes

Attributes in Python are variables that belong to an object and contain information about its properties and characteristics. There are two types of attributes. These are the class attribute and the instance attribute.

Conclusion

In conclusion, classes, objects, instances and attributes are all essential keys to coding in Python. It is therefore essential as developers to understand these terms and how they are used in Python.

Olamide Ayeni

Recent Posts

10 Reasons Why You Should Learn Python In 2025.

Introduction In the fast paced world of technology, learning a versatile and high-in-demand programming language…

3 days ago

Building And Implementing A Blog App Using Django: User Authentication

Introduction User Authentication policy is a very crucial process for every application and organization. It…

3 weeks ago

Building And Implementing A Blog App Using Django: Adding Forms

Introduction In previous articles, we have learnt about Django, how it works and how we…

1 month ago

Building And Implementing A Blog App Using The Django Framework

Introduction In this article, we shall learn how to build and implement a blog app.…

1 month ago

Building And Implementing A Message Board App Using Django

Introduction In this article, we shall use a database for the first time to build…

2 months ago

Building And Implementing A Two Paged Web Application Using Django

Introduction In this article, we will build a pages app that has a homepage and…

2 months ago