Concept of Classes



Hello folks...!!!
Today we shall move on to the concepts of Oops. I hope you all know the concept of classes and creating objects in C++. Similarly the concept of classes are applicable for python also. All the concepts like data abstraction,encapsulation are applicable here too.

How to define a class ?

syntax: 
                class class_name:
                                 variables initialization
                                 function_1(self,a,b)
                                 function_2(self,c,d)

Any number of functions can be defined inside a class. Initialization or constructor can also be defined inside a class. The concept of constructors will be discussed in later chapters.

To access the functions defined inside classes, instance of the class must be created. Instances are also known as objects of that class type. One class can contain any number of instances.

How to create instances of a class?

syntax: instance_variable_name=class_name()

How to access functions within classes ?

syntax: instance_variabe_name.function_name(parameters)

For each object a separate copy of each variable associated with the class is created.

The variables in the class can be accessed only if they are prefixed with 'self '. The reason behind doing this is, using 'self' denotes that the value for the variable associated with the current object has to be altered. The values for each object is pertained till the destruction of the object mostly.

All the functions defined within a class must have 'self ' parameter. The 'self ' denotes the current object that calls this function. While passing parameters to the functions within class, the first parameter is automatically considered to be the object, we need to pass only the rest of the arguments.

Let us look at some examples :

Python-oops-classes

  • Here the class name is operations. Let us consider that we need to increment the variable associated with the class by two. 
  • Three instances of the class has been created - x,y,z. For each instance initially the value of 'a' is '0'. The value changes and it is pertained until the object is destructed.
  • When the instance 'x' is used to call increment for the first time, a is incremented by 2 and stored back in a. That is why we use self.a which denotes current instance's 'a' value has to be altered.
  • When we again call increment() with object 'x', the restored value is incremented. That is why we get 4.
  • However we call the increment function for the first time with object 'y' and thus we get 2.
It is not necessary that the object parameter must be named 'self'. It can be any user defined name. For example :

python-oops-classes

Here, instead of 'self', I have used 'inst'. You can use any name.

Exercise:
Create a class for 'Fruits'. The attributes(variables) are color,taste.
Define two functions namely add_fruit and display for adding new fruits and displaying them respectively. Create instances of any three fruits and pass the values. Display them separately. 

Comments