Artificial Intelligence Design Machine Learning Technology

Python: Classes, Objects, Instances and Attributes?

Pinterest LinkedIn Tumblr

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.

ALSO READ  Creating Your First App In Django

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”.

ALSO READ  What is Clustering in Machine Learning Models

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.