Classes and Object in OOP in Python Programming
Classes and Object in Python: Both classes and object are the concepts of Object-Oriented Programming.
1). Class: A class is nothing but just a group of attributes and methods. Classes are user-defined. We can use all the concepts of procedural programming in class.
You will also get to know the difference between object and class in python by the end of this guide.
Attributes: We already learned about variables in our previous topics. Same like variables, attributes are represented by variables that are used to store some value or data.
Methods: We already learned about functions. Same like functions, methods perform action or tasks when needed. It is very similar to functions.
Classes may consist of the following Entities
- Methods: Already discussed about class methods above.
- Class variables: Class variables and attributes are same that we have already learned above.
About Objects & instance variable, we will discuss just after finishing our classes topic.
Syntax of creating a Class:
#Syntax of defining a class class class_name(object): #body of class #attributes #methods
Keyword “class” followed by class_name with round bracket: is used to define a class. Class_name may be anything. Example:
#body of class
So till now, we have seen the syntax of how to define a class. Let’s create a class:
#Example of class definition class Test: pass #This is an empty class with empty body
In example 1, we defined an empty class Test. But to use class or to use the attributes of class we need an Object.
2). Object: An object is nothing but just a variable that refers to a class and its elements. Without making an object of class we cannot revoke the attributes (class variables, class methods) of class. We can create at least one object of a class and more than one objects also based on our requirements. We will see both examples on object creation of a class (one object and two objects) Syntax of creating an object of a class.
Above is just an example of how to create an object of the class and how to access the elements or attributes of the class. Below is a simple example to understand the concepts more clear i.e. difference between object and class in python.
Here many of us are assuming that object_name is an object, but this is not that. Actually, it’s just a variable, and the real object is class_name(). Note down carefully, if the name of a class is XYZ then the object of that class is always XYZ(). We cannot use an object directly that’s why we use an object_name or variable. In other words, you can say that an object is nothing but just the blueprint of a class.
Now we will see some real example of classes and object.
#Class example class Test: x = "www.pythonlobby.com" obj = Test() print(obj.x)
This is just a simple program of class with having only one attribute ie. class variable.
Example 2: Program of class with having one class variable and class method.
#Class example class Test: #class_member or class_variable x = "www.pythonlobby.com" #class_method def add(self): #imp point to note "self" here is not a parameter. self.x = 21 self.y = 20 self.result = self.x+self.y print(self.result) #objects obj1 = Test() #object print(obj1.x) #calling class variable obj1.add() #calling class method
#Output #www.pythonlobby.com #41
3). Self: Here “self” is the new thing for us. Actually self is nothing but just a placeholder, where objects points. Let me clear you. As we already know that we can create more than one objects of any class. In fact, even we have only one object then also “self” is used but more used when number of objects more than one.
Like this one:
Here we have created three methods method1, method2, and saying.
Here why we used two objects. We used two objects because we are passing two names. If we use only one object and pass two values like this:
Then our output is:
Explanation: What we did is, in the first example, we created two objects and passed two values & it returned two results. And in the second example we passed two values to only a single object, what it did is: it just replaced the first value and returned the second one twice.
Self stores the object reference or the address of objects. If we create two objects then it has two different address. When we created two objects with value passing. Then compiler stored both values at different object locations. That’s why it returned both values. But in the second example, we created one object and when we stored the first value at that object location it is successfully stored but when the second value passed then it replaced the first one.
The object name obj1 is always storing some value, and that value is the memory location where our object is stored. If you want to know just use print(obj1) to know the memory location of your object. It will return you something like this <__main__.Test object at 0x00DA3FB8>. Your output will not be exactly like mine as memory locations always vary.