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?
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.
Lets take a look at this example. Lets create a class named FirstClass, with a property named y:
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:
This code will run without any error because of the pass statement used even without no content.
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 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.
m1 is an object of class Man. We shall now take a look at what Objects in Python are:
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.
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”.
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.
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:
In the illustration above, “name” and “occupation” are the instances of the function defined in class FirstClass.
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.
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.
Introduction Python is a programming language that has a large sum of frameworks. One of…
Introduction What is Cloud Computing? According to Wikipedia, Cloud Computing is a paradigm for enabling…
Introduction In the fast paced world of technology, learning a versatile and high-in-demand programming language…
Introduction User Authentication policy is a very crucial process for every application and organization. It…
Introduction In previous articles, we have learnt about Django, how it works and how we…
Introduction In this article, we shall learn how to build and implement a blog app.…