Python UNIT 5
Python UNIT 5
Classes and Objects: Python though it is a Partial Object Oriented Programming Language,
everything in python is treated as an object. For programmer convenience, the python supports
both structured programming system and also object oriented programming system. For working
with OOPS, one should have knowledge of 2 things, a class and an object. A Class is
combination of features that an object should possess. It is like a blue print of the object. The
object is a named instance of class that abstract all the features of the class, and the class features
are accessible only though object with a DOT operator called PERIOD operator.
#runner
e1=emp(5);
e2=emp(10);
print(e1.no)
print(e2.no)
output:
5
10
in above example, the first argument “self” refers the current object. i.e. when we created
object e1, the self refers e1 and when e2 is created the self refers object e2.
class variables and object variables: in python we can have 2 types of variables in objects. We
can have class variables and object variables. Class variables are common to all objects and the
objects can have their own individual variables declared. The objects variables have their scope
only within that object.
Ex:
class emp:
no=0
def __init__(self,no):
self.no=no
def create(self):
self.x=100;
e1=emp(5);
e2=emp(10);
e2.create();
print(e1.no.e1.x) # this line will have error. As the create() is not called for e1 and so e1 object
#doesn’t have the variable x within it.
print(e2.no,e2.x)
---
Q. Write object classes and objects in python
Q. write about different types of class methods and variables
Q. write about self argument in python
---
Python uses ‘_’ symbol to determine the access control for a specific data member or a member
function of a class. Access specifiers in Python have an important role to play in securing data
from unauthorized access and in preventing it from being exploited.
The members of a class that are declared private are accessible within the class only, private
access modifier is the most secure access modifier. Data members of a class are declared private
by adding a double underscore ‘__’ symbol before the data member of that class.
Ex:
class emp:
def __init__(self):
self.__pri=0
self.pub=10
e1=emp();
print(e1.pri) # this line raises error as pri is a private variable
print(e1.pub)
---
Q. Write about private and public variables of python
---
Private methods:
Encapsulation is one of the fundamental concepts in object-oriented programming
(OOP) in Python. It describes the idea of wrapping data and the methods that work on data
within one unit. This puts restrictions on accessing variables and methods directly and can
prevent the accidental modification of data. A class is an example of encapsulation as it
encapsulates all the data that is member functions, variables, etc. Now, there can be some
scenarios in which we need to put restrictions on some methods of the class so that they can
neither be accessed outside the class nor by any subclasses. To implement this private methods
come into play.
Consider a real-life example, a car engine, which is made up of many parts like spark plugs,
valves, pistons, etc. No user uses these parts directly, rather they just know how to use the
parts which use them. This is what private methods are used for. It is used to hide the inner
functionality of any class from the outside world. Private methods are those methods that
should neither be accessed outside the class nor by any base class. In Python, there is no
existence of Private methods that cannot be accessed except inside a class. However, to define
a private method prefix the member name with the double underscore “__”.
Ex:
class car:
def ignite(self):
print('ingited')
self.__start();
def __start(self):
print('started')
e=car();
e.ignite();
** we can not call the start() method of class car directly with object as it is a private method. It
can be called only within that class other methods.
---
Q. Write about private methods in python
---
The built-in class attributes provide us with information about the class.
Using the dot (.) operator, we may access the built-in class attributes.
The built-in class attributes in python are listed below –
Attributes Description
__doc__ If there is a class documentation class, this returns it. Otherwise, None
__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.
Ex:
# creating a class say Tutorialspoint
class Tutorialspoint:
'Welcome to Tutorialspoint Python'
def __init__(self):
print("The __init__ method")
# Printing the value of Built-in __dict__ attribute for the TutorialsPoint class
print(Tutorialspoint.__dict__)
ex:
# creating a class say Tutorialspoint
class Tutorialspoint:
'Welcome to Tutorialspoint Python'
def __init__(self):
print("The __init__ method")
def __init__(self):
print("The __init__ method")
# getting the name of the class using built-in __name__ class attribute
print(Tutorialspoint.__name__)
ex:
class Tutorialspoint:
'Welcome to Tutorialspoint Python'
def __init__(self):
print("The __init__ method")
# getting the module of the class using built-in __module__ class attribute
print(Tutorialspoint.__module__ )
class Tutorialspoint:
'Welcome to Tutorialspoint Python'
def __init__(self):
print("The __init__ method")
# getting the bases of the class using built-in __bases__ class attribute
print(Tutorialspoint.__bases__)
---
Q. explain different built in class attributes
---
Static methods:
In C++ and java, we use the keyword “static” to declare static features in a class. But in python
we use decorators to declare.
A decorator is prefixed with @ symbol. For example, a class method need to be declared as:
class <classname> :
@classmethod
def <method_name>() :
……..
Similarly, for static methods we use the decorator @staticmethod.
Syntax:
class <classname> :
@staticmethod
def <method_name>() :
……..
A static method does not receive an implicit first argument. A static method is also a method
that is bound to the class and not the object of the class. This method can’t access or modify
the class state. It is present in a class because it makes sense for the method to be present in
class.
We generally use the class method to create factory methods. Factory methods return class
objects ( similar to a constructor ) for different use cases.
We generally use static methods to create utility functions.
class MyClass:
def __init__(self, value):
self.value = value
@staticmethod
def get_max_value(x, y):
return max(x, y)
print(MyClass.get_max_value(20, 30))
print(obj.get_max_value(20, 30))
static methods can be accessed with class name and also with objects.
---
Q. Write about static methods in python
---