Tamilnadu Samacheer Kalvi 12th Computer Science Notes Chapter 10 Python Classes and Objects Notes
Class:
- Classes and Objects are the key features of Object Oriented Programming.
- A class is a way of binding data members and member function together.
- Class is the main building block in Python.
- Class is a template for the object.
Instantiation:
This process of creating object is called as
“Class Instantiation”.
Syntax:
Object_name = class_name()
Constructor in Python:
- In Python, there is a special function called “init” which act as a Constructor.
- It must begin and end with double underscore.
- This function will act as an ordinary function; but only difference is, it is executed automatically when the object is created.
- This constructor function can be defined with or without arguments.
- This method is used to initialize the class variables.
General format of _init_ method(Constructor function):
def _init_(self, [args ]):
< statements >
Destructor:
- Destructor is a special method gets executed automatically when an object exit from the scope.
- It is just opposite to constructor.
- In Python, _del_ ( ) method is used as destructor.
Class members:
- Variables defined inside a class are called as “Class Variable” and Functions defined inside are called as “Methods’:
- Class variable and methods are together known as members of the class.
- The class members should be accessed through objects or instance of class.
Defining class members:
Any class member (class variable or method (function)) can be defined and accessed by using object with a dot ( . ) operator.
Syntax:
Object_name .class_member
public and private data members of python class:
- The variables which are defined inside the class is public by default. These variables can be accessed anywhere in the program using dot operator.
- A variable prefixed with double underscore becomes private in nature. These variables can be accessed only within the class.
Object:
- Object is a collection of data and function that act on those data.
- Class is a template for the object.
- According to the concept of Object Oriented Programming, objects are also called as instances of a class or class variable.
- In Python, everything is an object.
Creating Objects:
- Once a class is created, next we should create an object or instance of that class.
- The process of creating object is called as “Class Instantiation”.
Syntax:
Object_name = class_name()
Accessing Class Members:
Any class member ie. class variable or method (function) can be accessed by using object with a dot (.) operator.
Syntax:
Object_name. class_member