Day 6 Assignment: 1) What Do You Mean by Decorators in Python?
Day 6 Assignment: 1) What Do You Mean by Decorators in Python?
Day 6 Assignment: 1) What Do You Mean by Decorators in Python?
add_one = plus_one
add_one(5)
Class methods
#decorator
@classmethod
def func(cls, arg1, arg2, ...):
....
Static method
#decorator
@staticmethod
def fun(arg1, arg2, ...):
...
3) what is the difference between @classmethod and @staticmethod?
4) Explain :
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.
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.
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.
@classmethod
def info(cls):
return cls.name
print(Student.info())
Output:
Student
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.
@staticmethod
def info():
return "This is a student class"
print(Student.info())
Output:
This a student class
5) Explain:
Variable declared at class level are called static variable which can be
accessed directly using class name.
class A:
...my_var = "shagun"
print(A.my_var)
Shagun
>>> 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"):
ob = Child3()
ob.func1()
Super() Function
ob = Child()
ob.func2()