Python can be used with or without Object Oriented Programming. What makes Python a strong language is the fact that it gives you the freedom to work the way you feel comfortable. However, it is very important to decide as to when one should opt for implementing object oriented programming.
Python is interpreted line by line which is why many people prefer to use it the way it is interpreted. To make the most out of Python it is best to identify objects. While working with only data it makes sense to stick to Python data structures but when we work on programs that are dynamic in nature, interactive programs, Application models, games etc where many times we have to deal with behaviour of entities and there is no stored data then it is a good idea to go for OOPS. An object oriented program will have classes which will consist of interacting objects. Every object can process data, and interact with other objects.
In Python you can define a class and in this class you can further define the attributes of an object; just as in case of any other object oriented programming language. You can also define class variables, data members, methods and yes, even inheritance is possible but for now we will just stick to the fundamentals of how a class is defined and how to define methods.
How to create a class:
- “class” statement is used to define a class. It should be followed by the name of the class and after the class name you must use colon ‘:’
- A class has a __init__() method. This is a special method that is called whenever an object is created. It is important to understand here that a method is different from function in the sense that it is defined within a class and the first parameter while defining a method is reference to the instance of the class and it is called “self”. However when a method of the class is called no value is passed for self.
The interesting thing that you need to know here is that “self” is not a reserved keyword, you can think of it as a strict rule. “self” refers to the newly created object of a class. Another important thing that you need to know is that the __init__() method is something similar but not exactly a constructor. You may have this doubt if you have sound knowledge any other object oriented languages. We say this because the object is created before __init__() method is called. However, there is no doubt about the fact that it is the __init__() method that is used to initialize the variables of an object.
So, with this information in mind we now proceed towards writing our first class in Python.
class Program(): def__init__(self,*args,**kwargs): self.lang = input(“What Language?: “) self.version = float(input(“Version?: ”)) self.skill = input(“What skill level?: “) p1 = Program()
Here “input” is used to prompt the user to provide an input value.
When you execute the program the user is prompted to provide information about language and the value is assigned to self.lang. Similarly self.version and self.skill take the next two values. Now if you access these values with p1.lang, p1.version and p1.self you will get the same values that you had provided.
GET YOUR FREE PYTHON EBOOK!