Day 6 Assignment: 1) What Do You Mean by Decorators in Python?

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Day 6 Assignment

1) WHAT DO YOU MEAN BY DECORATORS IN PYTHON?

 A decorator is a function that takes another function as its


argument, and returns yet another function.
 Decorators allow to modify the behavior of function or class. i.e. A
decorator allows a user to add new functionality to an existing
object without modifying its structure.
 Decorators are usually called before the definition of a function
you want to decorate

Eg. def plus_one(number):


return number + 1

add_one = plus_one
add_one(5)

2) WHAT IS the difference between class method and static method?

Class methods

 It can be created using the @classmethod decorator


 They are used to access the class itself but at the same time, they
are not capable of accessing individual instances.
 They are mostly useful when we need to create alternative
constructors, i.e. a class method that creates an instance of the
same class (that probably accepts slightly different arguments).
 Class methods are methods that are related to a class and have
access to all class-specific data.
 It returns a class method function.
 This method is also bound to the class but not to the object of the
class.
 This method can access the state of the class, therefore it can
modify the class state that will be applicable to all the instances.
 It takes cls as a parameter that points to the class and not to the
instance of the object.

E.g. class class_example(object):

#decorator
@classmethod
def func(cls, arg1, arg2, ...):
....

Static method

 It can be constructed through the @staticmethod decorator.


 Static methods are methods that are related to a class but do not
need to access any class-specific data.
 There is no need to instantiate an instance because we can simply
call this method.
 Static methods are great for utility functions. They are totally self-
contained and only work with data passed in as arguments.
 They are usually useful when we need to place a member to a
class just because it logically belongs to it.
 This method is bound to the class but not to the object of the
class.
 This method can not access or modify class state.
 It does not receive any implicit first argument, neither self nor cls.
 This method returns a static method of the function

E.g. class static_example(object):

#decorator
@staticmethod
def fun(arg1, arg2, ...):
...
3) what is the difference between @classmethod and @staticmethod?

Static Method Class Method


The @staticmethod decorator is used The @classmethod decorator is
to create a static method. used to create a class method.
No specific parameters are used. It takes cls as the first parameter.
It cannot access or modify the class It can access or modify the class
state. state.
Static methods do not know about
The class method takes the class
the class state. These methods are
as a parameter to know about
used to do some utility tasks by
the state of that class.
taking some parameters.
Static methods are used to do some Class methods are used for
utility tasks. factory methods.
It contains totally self-contained It can modify class-specific
code. details.

4) Explain :

>> Types of Methods

>>> instance method

This is a very basic and easy method that we use regularly when we
create classes in python. If we want to print an instance variable or
instance method we must create an object of that required class.

If we are using self as a function parameter or in front of a variable, that


is nothing but the calling instance itself.

As we are working with instance variables we use self keyword.


Instance variables are used with instance methods.

E.g. class Student:

def __init__(self, a, b):


self.a = a
self.b = b

def avg(self):
return (self.a + self.b) / 2

s1 = Student(10, 20)
print( s1.avg() )
Output:
15.0

In the above program, a and b are instance variables and these get
initialized when we create an object for the Student class. If we want to
call avg() function which is an instance method, we must create an
object for the class.

>>> Class method

classsmethod() function returns a class method as output for the given


function.
Syntax:
classmethod(function)
The classmethod() method takes only a function as an input parameter
and converts that into a class method.

There are two ways to create class methods in python:

 Using classmethod(function)
 Using @classmethod annotation

A class method can be called either using the class (such as C.f()) or
using an instance (such as C().f()). The instance is ignored except for its
class. If a class method is called from a derived class, the derived class
object is passed as the implied first argument.

In ClassMethod we use the cls keyword. Class variables are used with
class methods.

E.g. class Student:


name = 'Student'
def __init__(self, a, b):
self.a = a
self.b = b

@classmethod
def info(cls):
return cls.name

print(Student.info())
Output:
Student

In this, name is a class variable. If we want to create a class method we


must use @classmethod decorator and cls as a parameter for that
function.

>>> Static method

A static method can be called without an object for that class, using the
class name directly. If you want to do something extra with a class we
use static methods.
For example, If you want to print factorial of a number then we don't
need to use class variables or instance variables to print the factorial of
a number. We just simply pass a number to the static method that we
have created and it returns the factorial.

E.g. class Student:


name = 'Student'
def __init__(self, a, b):
self.a = a
self.b = b

@staticmethod
def info():
return "This is a student class"

print(Student.info())

Output:
This a student class

5) Explain:

>> types of variables

>>> class/static variable

Variable declared at class level are called static variable which can be
accessed directly using class name.

 Class or Static variables are shared amongst objects of the class.


 All variables which are assigned a value in the class declaration
are class variables.
 And variables which are assigned values inside class methods are
instance variables.

class A:
...my_var = "shagun"

print(A.my_var)
Shagun

>>> instance variable

Variables that are related and accessed by instance of a class are


instance variables.

>>> a = A()
>>> a.my_var = "pruthi"
>>> print(A.my_var,a.my_var)
shagun pruthi

6) Inheritance Programs

Inheritance allows us to define a class that inherits all the methods and
properties from another class.

 Parent class is the class being inherited from, also called base
class.
 Child class is the class that inherits from another class, also called
derived class.

SYNTAX:
class derived-class(base class):
<class-suite>

Syntax
class derive-class(<base class 1>, <base class 2>, ..... <base class n>):
<class - suite>

A class can inherit multiple classes by mentioning all of them inside the
bracket.

Types:

 Single Inheritance:
When a child class inherits only a single parent class.

class Parent:
def func1(self):
print("this is function one")
class Child(Parent):
def func2(self):
print(" this is function 2 ")
ob = Child()
ob.func1()
ob.func2()

 Multiple Inheritance:
When a child class inherits from more than one parent class.
class Parent:
def func1(self):
print("this is function 1")
class Parent2:
def func2(self):
print("this is function 2")
class Child(Parent , Parent2):
def func3(self):
print("this is function 3")

ob = Child()
ob.func1()
ob.func2()
ob.func3()

 Multilevel Inheritance:
When a child class becomes a parent class for another child class.
class Parent:
def func1(self):
print("this is function 1")
class Child(Parent):
def func2(self):
print("this is function 2")
class Child2(Child):
def func3("this is function 3")
ob = Child2()
ob.func1()
ob.func2()
ob.func3()
 Hierarchical Inheritance:
Hierarchical inheritance involves multiple inheritance from the
same base or parent class.
class Parent:
def func1(self):
print("this is function 1")
class Child(Parent):
def func2(self):
print("this is function 2")
class Child2(Parent):
def func3(self):
print("this is function 3")

ob = Child()
ob1 = Child2()
ob.func1()
ob.func2()

 Hybrid Inheritance:
Hybrid inheritance involves multiple inheritance taking place in a
single program.

class Parent:
def func1(self):
print("this is function one")

class Child(Parent):
def func2(self):
print("this is function 2")

class Child1(Parent):
def func3(self):
print(" this is function 3"):

class Child3(Parent , Child1):


def func4(self):
print(" this is function 4")

ob = Child3()
ob.func1()

Super() Function

Super function allows us to call a method from the parent class.


class Parent:
def func1(self):
print("this is function 1")
class Child(Parent):
def func2(self):
Super().func1()
print("this is function 2")

ob = Child()
ob.func2()

You might also like