Python Acadamic UNIT 5 Assignment and Mid Exam Solutions
Python Acadamic UNIT 5 Assignment and Mid Exam Solutions
Definition
Class is a collection of attributes (variables and functions) together in a single unit.
In oop, variables are also called data members, functions are also called as methods.
The collection of class members (data and methods) together called as class entity.
Syntax
class class_name:
member1
member2
………
Importance of class
Class provides encapsulation and data hiding mechanism.
Accessing class data is restricted from the outside world using access modifiers (public
private).
Once the class (template) is ready, by using which we can create multiple copies of same
type of objects (we can consider class is like a factory for making objects).
Static methods can be accessed with the class name without creating an object to the
class.
#Example
Write a program to demonstrate class and show how to access class members
class shape:
@staticmethods
def circle():
return("circle")
def triangle():
return("triangle")
def rectangle():
return("rectangle")
Definition
Instance of a class is called object.
Importance of object:
Object is needed to access class data.
Without Object we cannot access instance variables and instance methods.
Object invokes constructor method to load class data into main memory during program
execution.
Accessing Class methods using object must have the first argument named as self.
The self argument refers to the object itself. Python provides current class object
reference as an argument value to the self automatically.
Syntax
object_name = class_name()
#Example
Create a template called shape and define triangle, rectangle, circle methods in it and return its
name as a result.
class shape:
def circle(self):
return("circle")
def triangle(self):
return("triangle")
def rectangle(self):
return("rectangle")
1) Single inheritance
2) Multi Level inheritance
3) Multiple inheritance
4) Multipath or hybrid inheritance
Single Inheritance
Multiple Inheritance
Example (Program)
Constructor
Constructor allocates the resources (memory) to class members.
It is a special method defined in a class either by user (explicitly) or by the python
interpreter (implicitly), if user not provided.
It is automatically (implicitly) invoked when we create an object to the class.
It is identified with the name __init__()
Constructor method needs self as an argument as it is invoked with an object.
It is mainly used to initialize class data (instance variables or class variable).
Super ().__init__() method is used to invoke constructors explicitly in the case of
inheritance.
Destructor
It is used to de allocate the resources occupied by an object (constructor)
It is a special method written automatically by the python interpreter (implicitly), if
programmer not defined explicitly.
It is invoked automatically (implicitly) when object is going out of scope.
It is identified with the name __del__()
Example
Write a program to initialize class variable and instance variable with an example program
using constructor.
7) Explain the concept of operator overloading with an example program?
Operator Overloading
The predefined operators (+,-,*…) existed in any programming language performs
operation between operands if they are valid types.
These operators cannot works with user defined types like objects.
To perform operation between user defined types, python allows the programmer to
extend the features of existing operators such as +,-,*… by using concept called operator
overloading.
Example:
4 + ‘A’ It doesn’t works, because they are invalid types and it raises an
exception TypeError
NOTE:
If you observe clearly the same operator (+) between integer and string is
performed without any restriction or by raising an error.
This type of concept is called polymorphism.
The same operator is changing its behavior based on its input type (i.e.,
between integers and strings)
Requirement