Professional Documents
Culture Documents
Decorator Part1 notes
Decorator Part1 notes
DECORATORS -
----------
OR
1)A decorators function must be an nested function were in the outer function
must take only one argument (Decorator function address)
@outer
def main_func():
statements
return statement
print(main_func(arguments))
example:------>
# a,b=b,a #2,4=4,2
# return inner
# @outer
"""
1.def outer ----> memory address will be created
3. func == div
5. def inner()-----> memory address will be created and it will return this address
to the func which made it call
7. next we will call the div (2,4) ----> internally invoking the inner func and
(a---> 2 , b----> 4)
10. func(4,2) -----> called so it will invoke ----> main func()---> div(4,2)
11. then it will perform the statement inside the main function ---> div()
a/b ----> 4/2 -----> 2.0
"""
1. def addition -----> address is get created
3. func= add
5. def inner() ------> memory address will be created and it will return to the
func which called it
8. all the values you passed when calling the add () will go store in the inner
(*args)
11. add(*args) ----> it will perform sum of all the arguments it is getting stored
in variable "c"
13. controller will go inside the inner() and it will print the next line which is
"addition performed"
# def outer(func):
# def inner(*args,**kwargs):
# res = func(*args,**kwargs) #res = -22
# return (abs(res))
# return inner
# @outer
# def sub(a,b):
# return a-b
# print(sub(-20,2))
"""
1. def outer () ----> memory address will be created
3. func == sub
5. def inner () ----> memory address will be created and it will return to the func
which made it call
9. it will perform the action ---> a-b ----> -20-2----> -22 , since you are
returning the value
11. abs(-22)---> 22 -----> res it will come to the main function and print it out
"""
# def outer(func):
# def inner():
# execution_time = end-start
# return inner
#
# @outer
# def display():
# sleep(3)
# for i in range(1,10):
# ...
# display()
"""
1. def outer () ----> memory address will be created
3. func == display
5. def inner () ----> memory address will be created and it will return to the func
which made it call
10. before executing the statements inside the main function it is sleeping for 3
seconds
11. for loop will execute ----> then end = time.time() again it will take the
laptop time and store it in end variable
12. execution time - end - start ----> it will gove the execution time
13. func.__name__ ----> here we have used __name__ magic method because we want
the function name
"""
"""
1. def outer () ----> memory address will be created
2. @outer - display= outer(display)
3. func == display
4. outer() -----> will be called ---> controller will go inside
5. def inner () ----> memory address will be created and it will return to the func
which made it call
6. display = inner () address
7. display() ----> call and arguments will be passed (2,3) and internally it is
invoking the inner func() arguments will be stored in *args
8. for i in args ---> it will traverse inside the args which will be in tuple form
9. next it will print the type of (i)
10. func is called which will invoke the display func()
11. it will come inside the func and it will sum the args ---->(2,3)
"""
#6. WAP TO SUM THE POSITIONAL ARGUMENTS AND GET THE LENGTH OF THE KEYWORD ARGUMENTS
# def pos_key_modify(func):
# def wrapper(*args,**kwargs):
# print(f"the total sum of args are : {sum(args)}")
# func(*args,**kwargs)
# return wrapper
# @pos_key_modify
# def pos_key(*args,**kwargs):
# print(f"the length of keyword arguments are : {len(kwargs)}")
# pos_key(2,4,5,a=1,b=2,c=3)
"""
1. def pos_key_modify() ----> memory address will be created
2. @pos_key_modify - pos_key= pos_key_modify(pos_key)
3. func == pos_key
4. pos_key_modify() -----> will be called ---> controller will go inside
5. def wrapper() ----> memory address will be created and it will return to the
func which made it call
6. pos_key = wrapper() address
7. pos_key(2,4,5,a=1,b=2,c=3) it is called ----> invoking the wrapper ()
8. print(f"the total sum of args are : {sum(args)}") ----> this will get executed
giving output --> 10
9. func() ---> called it invokes the pos_key
10 print(f"the length of keyword arguments are : {len(kwargs)}")---> this will get
executed giving out ---> 3
"""
#7. WADF TO DO ARTHEMATIC OPERATIONS ON THE GIVEN NUMBER AND THE CONDITION IS A>B
THEN PERFORM MULTIPLICATION IN DECORATOR FUNCTION ELSE CUBE IT IN DECORATOR
# def outer(func):
# def inner(a,b):
# if a>b:
# print(f"performing multiplication {a*b}")
# else:
# print(f"performing cubing of the number {a**3}, {b**3}")
# func(a,b)
# return inner
#
# @outer
# def arth(a,b): #a=5 , b=3 if a>b
# print(f"performing addition {a+b}") #8
# print(f"performing subtraction {a-b}") #2
# arth(5,3)
"""
1. def outer () ----> memory address will be created
2. @outer - arth= outer(display)
3. func == arth
4. outer() -----> will be called ---> controller will go inside
5. def inner () ----> memory address will be created and it will return to the func
which made it call
6. arth = inner () address
7. arth ---> called and arguments will be passed (5,3) ---> inner function will get
invoked inner(a,b) ---> a=5 , b=3
8. if condition will check if a>b ---> condition is true
9. it will print (a*b)
10. func(5,3) ---> called ---> it will invoke arth (5,3)
11. addition and subtraction will be performed and printed
"""
"""
2. DELAY DECORATORS
--------------------
"""
#1. WADF TO CHECK HOW MUCH TIME IT IS TAKING TO PERFORM EACH TASK
#I WILL BE PERRFORM TWO TASK
# print("EMPLOYEE DETAILS")
# def outer(func):
# def inner(*args,**kwargs):
# sleep(3)
# func(*args,**kwargs)
# return inner
#
# @outer
# def display_name():
# res = input("enter the employee name :")
# print(f" EMPLOYEE NAME : {res}")
#
# @outer
# def display_email():
# res1 = input("enter the employee email :")
# print(f" EMPLOYEE EMAIL : {res1}")
#
# @outer
# def display_phno():
# res2 = int(input("enter the employee phone number :"))
# print(f" EMPLOYEE PHONE NUMBER : {res2}")
# display_name()
# display_email()
# display_phno()
# def outer(func):
# def inner(*args,**kwargs):
# print(f"the decorated function is {func.__name__}")
# res = func(*args,**kwargs)
# return res
# return inner
#
# @outer
# def display(num):
# if num % 2 ==0: #1% 2 == 0
# return "num is even"
# else:
# return "num is odd"
# print(display(1))
# print(display(2))
# def count_(func):
# def wrapper(*args):
# print(f"number of arguments passed to a function :{len(args)}")
#
# func(*args)
#
# return wrapper
#
# @count_
# def add_(a,b,c,d,e):
# print(a+b+c+d+e)
#
# add_(1,2,3,4,5)
#6. wadf to print "Guys Respond" message before executing any function by useing
#multiple calls.
#
# import time
# def message(func):
# def inner(*args):
# print("guys respond")
# func(*args)
# return inner
#
# @message
# def mul(a,b):
# time.sleep(5)
# print("mul",a*b)
# mul(10,2)
#
# @message
# def div(a,b):
# time.sleep(5)
# print("div",a/b)
# div(10,2)
#
# @message
# def add(a,b):
# time.sleep(5)
# print("add",a+b)
#
# add(10,2)
#7..wadf to print a message "Enter OTP" to confirm "before doing Tranction and
#withdrawal.
# def confirm(func):
# def tranction(*args):
# print("enter otp to confirm")
# func(*args)
# return tranction
#
# @confirm
# def withdrawal(amount):
# print(f"you have withdrawal rs.{amount}")
# withdrawal(5000)
# @confirm
# def balance(amount):
# print(f"the current balance is rs.{amount}")
# balance(10)
#
# @confirm
# def c_pin(pin):
# print(f"succesfully changed your pin {pin}")
# c_pin(1432)
multiple decorators:----->
# def guys(func):
# def inner(*args):
# print("good morning guys") #decorator func1
# func(*args)
# return inner
#
# def student(func):
# def wrapper(*args):
# print("good night") #decorator func2
# func(*args)
# return wrapper
#
# def python(func):
# def display(*args):
# print("its rainy day")
# func(*args)
# return display
#
# @guys
# @student
# @python
# def greet():
# print("good evening") #main function
# greet()
# def PIN(func):
# def inner (*args):
# Pin=eval(input("Enter PIN"))
# func(*args)
# return inner()
# @PIN
# def Tranction():
# print("your cash is 200")
# @PIN
# def Balance():
# print("Your balance is 1000")
#
# @PIN
# def Pinchange():
# print("PIN changed")
# def log(func):
# def inner(*args,**kwargs):
# print("i am logging")
# func(*args,**kwargs)
# return inner
#
# def register(func):
# def wrapper(*args,**kwargs):
# print("i have registered")
# func(*args,**kwargs)
# return wrapper
# @log
# @register
#
# def Employee(name,reg):
# print(name,reg)
# name=eval(input("pass name:"))
# reg=eval(input("pass email id"))
# Employee(name,reg)
# pass name:"rekha"
# pass email id"rekha@143gmail.com"
output:-->
# i am logging
# i have registered
# rekha rekha@143gmail.com