This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Function with arguments | |
@ company | |
def office_hours(start_time,finish_time): | |
return ('Hi! Your office hours will be ' + str((int(finish_time.split(':')[0]) - int(start_time.split(':')[0])))+' hours.') | |
print(office_hours('10:00','18:00')) | |
#[Output]: | |
#'Hi! Your office hours will be 8 hours.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Now this nested function will be able to handle any no. of arguments - zero, one , so on arguments and also able to handle ## key value pair arguments as well. | |
def company(decorated_function): | |
## Importing logging module | |
import logging | |
## Creating a file to store the logging info | |
logging.basicConfig(filename= 'log_file.log',level=logging.INFO) | |
## *args => any no. of aruguments ## **kwargs => any no. of key value pair value or dictionary values | |
def wrapper_function(*args, **kwargs): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Now this nested function will be able to handle any no. of arguments - zero, one , so on arguments and also able to handle | |
## key value pair arguments as well. | |
def company(decorated_function): | |
## *args => any no. of aruguments | |
## **kwargs => any no. of key value pair value or dictionary values | |
def wrapper_function(*args, **kwargs): | |
print('Wrapper function is executed first') | |
return decorated_function(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ company | |
def office_hours(start_time,finish_time): | |
return ('Hi! Your office hours will be ' + str((int(finish_time.split(':')[0]) - int(start_time.split(':')[0])))+ | |
' hours.') | |
## Here it is giving the error because our wrapper function will not be able to handle any no.of arguments | |
print(office_hours('10:00','18:00')) | |
#[Output]: | |
#--------------------------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ company | |
def new_employee(): | |
print('Hi! Welcome to Robofied team') | |
new_employee() | |
#[Output]: | |
#Wrapper function is executed first | |
#Hi! Welcome to Robofied team |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" defining a nested function, in that inner function i.e, a wrapper function is calling the passed function in outer function | |
""" | |
def company(decorated_function): | |
def wrapper_function(): | |
print('Wrapper function is executed first') | |
## calling the decorator function | |
return decorated_function() | |
return wrapper_function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def office_hours(start_time,finish_time): | |
return ('Hi! Your office hours will be ' + str((int(finish_time.split(':')[0]) - int(start_time.split(':')[0])))+ | |
' hours.') | |
args = ('10:00','18:00') | |
print(office_hours(*args)) | |
#[Output]: | |
#'Hi! Your office hours will be 8 hours.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## function which is taking multiple or any no. of agruments | |
## **kwargs takes input in the form of dictionary only. | |
def multiple_args(**kwargs): | |
## printing key and value pair. | |
for key, value in kwargs.items(): | |
print('Key is {0} and value is {1}'.format(key,value)) | |
## creating a dictionary to be passed in the function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Creating a function which can take multiple(any no. of ) arguments | |
## Here in this example it is not much necessary to give unknown number of parameters | |
## But in some cases we are not knowing then this *args is very useful | |
def office_hours(*args): | |
return ('Hi!'+ str(args[0]) +' Your office hours will be ' + str((int(args[2].split(':')[0]) - int(args[1].split(':')[0])))+ | |
' hours.') | |
## calling the above function | |
office_hours('Rajesh','10:00','18:00') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def office_hours(start_time, finish_time): | |
s = start_time | |
f = finish_time | |
def cal_working_hours(): | |
return ('Hi '+ '! Your office hours will be ' + str((int(f.split(':')[0]) - int(s.split(':')[0])))+ | |
' hours.') | |
## returning the function but without () | |
return cal_working_hours |