Python Tutorials
- Python
- How to install Python?
- PIP
- How to Run Python Program
- Python Identifiers,Statement, Indentation and Comments
- Variable
- Data type
- Decision Making
- Python Loops
- Break,Continue, Pass
- Functions
- Predefine Functions
- Lambda Functions
- Variable Scope
- List
- Tuple
- Python Sets
- Python Dictionary
- Python String
- String Formating
- Input/Output
- File Handling (Input / Output)
- Iterators
- Python Modules
- Python Date
- Python JSON
- Classes and Objects
- Constructor
- Polymorphism
- Encapsulation
- inheritance
- Class or Static Variables in Python
- class method vs static method
- Abstraction
- Exception Handling
- MySql Python
- MySql Create Database
- MySql CRUD
- Django
What is Class & Object in Python?
Python is an object oriented programming language.
Major principles of object-oriented programming system are given below.
Class :-
The class can be defined as a collection of objects. It is a logical entity that has some specific attributes and methods. For example: if you have an employee class, then it should contain an attribute and method, i.e. an email id, name, age, salary, etc.
The class statement creates a new class definition. The name of the class is created by the keyword class followed by a colon
A class is a blueprint for the object.
File name : index.py
class MyClass:
x = 5
Object :-
The object is an entity that has state and behavior. It may be any real-world object like the mouse, keyboard, chair, table, pen, etc.
When we define a class, it needs to create an object to allocate the memory.
File name : index.py
<object-name> = <class-name>(<arguments>)
class MyClass:
x = 5
obj = MyClass();
print(obj.x)
Here, obj is an object of class MyClass.
Example
File name : index.py
class car:
def __init__(self,modelname, year):
self.modelname = modelname
self.year = year
def display(self):
print(self.modelname,self.year)
c1 = car("Toyota", 2016)
c1.display()
Output:
Toyota 2016
we have created the class named car, and it has two attributes modelname and year. We have created a c1 object to access the class attribute. The c1 object will allocate memory for these values
Example
File name : index.py
class Employee:
'Common base class for all employees'
empCount = 0
def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1
def displayCount(self):
print "Total Employee %d" % Employee.empCount
def displayEmployee(self):
print "Name : ", self.name, ", Salary: ", self.salary
Example
File name : index.py
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
__init()__
The __init__() function is called automatically every time the class is being used to create a new object.
The first method __init__() is a special method, which is called class constructor or initialization method that Python calls when you create a new instance of this class.
File name : index.py
class Employee:
id = 10
name = "Devansh"
def display (self):
print(self.id,self.name)
Here the self is used as a reference variable, which refers to the current class object. It is always the first argument in the function definition. However, using self is optional in the function call.
self parameter :- The self-parameter refers to the current instance of the class and accesses the class variables. We can use anything instead of self, but it must be the first parameter of any function which belongs to the class.
File name : index.py
Delete the Object :-
We can delete the properties of the object or object itself by using the del keyword.
File name : index.py
class Employee:
id = 10
name = "John"
def display(self):
print("ID: %d \nName: %s" % (self.id, self.name))
# Creating a emp instance of Employee class
emp = Employee()
# Deleting the property of object
del emp.id
# Deleting the object itself
del emp
emp.display()
File name : index.py
File name : index.py
File name : index.py
File name : index.py
File name : index.py
File name : index.py
File name : index.py