Selectedreference - Python Classes - Reference
Selectedreference - Python Classes - Reference
Introduction
The basic idea behind an object-oriented programming
(OOP) is to combine both data and associated
procedures (known as methods) into a single unit which
operate on the data. Such a unit is called an object.
Local Variables:
Output:
a is -> 40
After new value within the function a is -> 50
Value of a is -> 100
Explanation:
def function_local():
global a
print('a is -> ',a)
a = 50
print('After new value within the function a is -> '
a = 100
function_local()
print('Value of a is ->',a)
Output:
a is -> 100
After new value within the function a is -> 50
Value of a is -> 50
Explanation:
nonlocal statement
def outside():
a = 10
def inside():
a = 20
print("Inside a ->", a)
inside()
print("outside a->", a)
outside()
def outside():
a = 10
def inside():
nonlocal a
a = 20
print("The value of a in inside() function - "
inside()
print("The value of a in outside() function - ", a)
outside()
Defining a class:
class Student:
Statement-1
Statement-1
....
....
....
Statement-n
Creating a Class:
class Student:
stu_class = 'V'
stu_roll_no = 12
stu_name = "David"
Class Objects:
class Student:
"""A simple example class"""
stu_class = 'V'
stu_roll_no = 12
stu_name = "David"
def messg(self):
return 'New Session will start soon.'
class Student:
"""A simple example class"""
def __init__(self, sclass, sroll, sname):
self.c = sclass
self.r = sroll
self.n = sname
def messg(self):
return 'New Session will start soon.'
Operators
Addition:
class School:
def __init__(self, *subjects):
self.subjects = list(subjects)
class Subject:
Output:
Output:
WindowsPath('C:/Users/User/AppData/Local/Programs/Py
Greater / Equality:
class Person:
'''person entity'''
def __repr__(self):
return f'Person(first_name={self.first_name}, surname={
Output:
Length:
class School:
def __init__(self, *subjects):
self.subjects = list(subjects)
def __len__(self):
return len(self.subjects)
Sub = School(Subject(), Subject())
print(len(Sub))
Output:
Getitem:
class School:
def __init__(self, *subjects):
self.subjects = list(subjects)
Output:
Inheritance:
class DerivedClassName(BaseClassName):
Statement-1
Statement-1
....
....
....
Statement-n
class DerivedClassName(BaseClassName1,
BaseClassName2, BaseClassName3):
Statement-1
Statement-1
....
....
....
Statement-n
Example:
class CompanyMember:
'''Represents Company Member.'''
def __init__(self, name, designation, age):
self.name = name
self.designation = designation
self.age = age
def tell(self):
'''Details of an employee.'''
print('Name: ', self.name,'\nDesignation : '
class FactoryStaff(CompanyMember):
'''Represents a Factory Staff.'''
def __init__(self, name, designation, age, overtime_all
CompanyMember.__init__(self, name, designation
self.overtime_allow = overtime_allow
CompanyMember.tell(self)
print('Overtime Allowance : ',self.overtime_all
class OfficeStaff(CompanyMember):
'''Represents a Office Staff.'''
def __init__(self, name, designation, age, travelling_a
CompanyMember.__init__(self, name, designation
self.marks = travelling_allow
CompanyMember.tell(self)
print('Traveling Allowance : ',self.travelling_
Now execute the class in Python Shell and see the
output.
Previous: calendar()
Next: Python Built-in Functions