Recents in Beach

Python OOP(Class and Object)

PYTHON Class and Object


Introduction

Python is an object-oriented programming language.so, its main focus is on objects
unlike Procedure oriented programming language which mainly focuses on Function.
In object-oriented programming language Object is simply a collection of data(variable)
and methods(Function) that acts on those data.
we can easily create and use classes and objects

Python Class

A user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attribute are data members(class variable and instance variables) and methods,accessed via dot notation.
A class is a blueprint for the object. Let's understand it by an example:

Suppose a class is a prototype(Blueprint) of a building. A building contains all the details about the floor, doors, windows, etc. we can make another buildings (as many as we want) based on these details. So building is a class and we can create many objects from a class.

  • Class is used when you want to use the same code at different palaces in Program then just call the class name with desired attributes.This save the time to rewrite the code and there are also other use of class in python.

Python Object

A unique instance of a data structure that is defined by its class.An object comprises both data members (class variable and instance variables) and methods.
An object is also called an instance of a class and the process of creating this object is known as 
instantiation.Python classes contain all the standard features of Object Oriented Programming. 

Creating Classes

The class statment creates a new class definition.The name of the class after it a colon as follow(class name follow keyword name).
 class followed by a colon as follows −

  • This class has a documentation string, which can be accessed via classname.__doc__

  • The class_suite consists of all the component statements defining class variables,instance variable,data, attributes and methodes.

Example:
output of above code:


Explanation:

  • The first methode  __init__() is a special methode, which is called class constructor or initialization methode that Python calls when you create a new object.
  • You declare other class methods like normal functions(like Floor() in above example) with the exception that the first argument to each method is self. Python adds the self argument to the list for you; you do not need to include it when you call the methods.

Creating Instance Objects


To create instances of a class, you call the class using class name and pass in whatever arguments its __init__ method accepts. Lets Create building class object of above example :


Accessing Attributes

You access the object's attributes using the dot operator with object.Class variable would be accessed  as follows
output of above code:



Instead of using the normal statements to access attributes, you can use the following functions..

getattr(obj, name[, default]) − to access the attribute of object. 
hasattr(obj,name) − to check if an attribute exists or not. 
setattr(obj,name,value) − to set an attribute. If attribute does not exist, then it would be created. 
delattr(obj, name) − to delete an attribute.

Example:


Built-In Class Attributes

Every Python class keeps following built-in attributes and they can be accessed using dot operator like any other attribute −

__dict__  : Dictionary containing the class's namespace. 
 __doc__ : Class documentation string or none, if undefined. 
 __name__ : Class name. 
 __module__  : Module name in which the class is defined. This attribute is "__main__" in interactive mode. 
__bases__ : A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list.
let us try to access all these attributes
output of above code:

Some terms related to Class and Object

Data member :
A class variable or instance variable that holds data associated with a class and its objects are called Data member. 
Data Variable
Inside the red box are called data member


Class variable  :
A variable that is shared by all instances of a class. Class variables are defined within a
class but outside any of the class's methods. Class variables are not used as frequently as instance variables are. 
Class Variable
Inside the red box are called class variables


Instance variable :
A variable that is defined inside a method and belongs only to the current instance of a class(Object). 
Inside the red box called Instance variables


  • Class Variables are same for all objects of a definite class but Instance variable are different for different objects of a definite class.
For example:
output of above code:


Instance :
An individual object of a certain class. An object obj that belongs to a class Circle, for example, is an  instance of the class Circle. 

Instantiation :
The creation of an instance of a class(Object). 

Method :
A special kind of function that is defined in a class definition like Floor() in Data member example. 

Function overloading :
The assignment of more than one behavior to a particular function. The operation performed 
varies by the types of objects or arguments involved.

Operator overloading :
The assignment of more than one function to a particular operator. For example, the + operator will, perform arithmetic addition on two numbers, merge two lists and concatenate two strings. This feature in Python, that allows the same operator to have different meaning according to the context is called operator overloading.

Inheritance :
The transfer of the characteristics of a class to other classes that are derived from it. 

Polymorphism :
Polymorphism is made by two words "poly" and "morphs". Poly means many and Morphs means form, shape. It defines that one task can be performed in different ways.

Encapsulation :
Encapsulation is also the feature of object-oriented programming. It is used to restrict access to methods and variables. In encapsulation, code and data are wrapped together within a single unit from being modified by accident.

Data Abstraction :
Abstraction is used to hide internal details and show only functionalities. Abstracting something 
means to give names to things, so that the name captures the core of what a function or a whole program does.


                                      Thank You



Post a Comment

0 Comments