Python is one of the easiest programming languages to either newbies or seasoned professionals alike. If you are completely new to programming language, you should be able to pick up the concepts at a quicker pace and if you already have some coding knowledge (whatever might be the programming language that you have already learnt), learning Python is just a piece of cake.
If you’re here then we assume that you are very much interested in getting your hands dirty with the Python programming skills, so why delay? Let us deep dive into the basics of the programming language. The basics of any programming languages (C++ or Java etc) are the CLASSES and OBJECTS of the classes. The following section deals in great detail about these concepts, so let us get started.
In general object oriented programming notation, a class is a container of your data and the methods that perform actions on these data. In other words, a class is the one that bundles your data and the functionality together as a single unit. Once a class is created, you create instances of the class for your programmatic usage which are then termed as objects. You could understand the class to be equivalent to a template with empty placeholders and objects to be photocopies of the template but the only difference is that each photocopy has its own related details added to it, than that are available on the class itself.
As already mentioned above that Python is an object oriented programming language, the main focus remains on classes and objects rather than on functions as present in any procedure oriented programming language. A class can be defined in Python programming language using the class keyword. A class that we create using Python should always have a docstring (a string to define and describe what the class is for?), not mandatory but it is good to have one.
Let us now take a look at a sample class definition in Python programming language:
class MyFirstPythonClass :
“This is a docstring to explain that it is my very first Python class”
A class once created will create a new local namespace where all the class’s attributes (data or functions) are defined. As soon as we define a class, an object of same name will also be created which will be helpful to allow us access the data or functions on that object. You can then create multiple other objects of the class as like this:
newObject = MyFirstPythonClass()
Now let us take a look at an example of creating a class in Python and at the same time, let us create an object of the class created and also check whether we are able to access the data and the functions of the class. The example below will give you all the necessary details in a single shot, as to how we can create a class, create an object out of it, how to use data and functions of a class via the object that we have newly created.
Related Page: Classes And Objects - Python
In the class definition, you would have observed that the function had an argument named self but the method invocation via the object didn’t use any argument while invoking it. This is because, whenever a method is called from an object, the object becomes the first argument to the method by default. The method call can safely by transformed as the following for better understanding.
newObject.function() -> MyFirstPythonClass.function(newObject)
Along with the points mentioned above, we need to focus on methods that being with double underscores, as these are all special functions with specific meaning. A constructor method __init__() will always be called the first whenever a new object is created out of a class.
Let’s extend the above example to incorporate a constructor to assign default values whenever there are any new objects created out of this class especially.
As discussed earlier, the double underscored special function called the constructor of a class is always an important function that gets executed whenever there is a new object is created out of the class. There are two objects that we have created out of the class that we have created and the constructor will be executed twice to instantiate the values of the class variables in each of the case.
Related Page: Regular Expression Operations-Python
Check also the ease of usage, the first object was with two parameters as specified in the class but the second object that was created had an additional class variable added to it.
In this article, we have seen what Python is and at the same time tried to understand its importance and its ease of use. Taking a step further, we also have tried to understand the concept of classes and objects in the Python programming language. Hope that you have understood the concepts quite well and if you still require any further details on these topics, one of the best resources to rely on is the Python documentation.
Get Updates on Tech posts, Interview & Certification questions and training schedules