Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Decorators in python

# decorator function to convert to lowercase
def lowercase_decorator(function):
   def wrapper():
       func = function()
       string_lowercase = func.lower()
       return string_lowercase
   return wrapper
# decorator function to split words
def splitter_decorator(function):
   def wrapper():
       func = function()
       string_split = func.split()
       return string_split
   return wrapper
@splitter_decorator # this is executed next
@lowercase_decorator # this is executed first
def hello():
   return 'Hello World'
hello()   # output => [ 'hello' , 'world' ]
Comment

decorators in python

# this functon converts any string into uppercase
def deco(function):
    def wrap(s):
        return s.upper()
        function(s)
    return wrap

@deco
def display(s):
    return s 
print(display("not bad"))
Comment

examples of function decorators in Python

from functools import wraps

def logit(func):
    @wraps(func)
    def with_logging(*args, **kwargs):
        print(func.__name__ + " was called")
        return func(*args, **kwargs)
    return with_logging

@logit
def addition_func(x):
   """Do some math."""
   return x + x


result = addition_func(4)
# Output: addition_func was called
Comment

decorators in python

def deco(function):
    def wrap(num):
        if num % 2 == 0:
            print(num,"is even ")
        else:
            print(num,"is odd")
        function(num)
    return wrap
    
@deco
def display(num):
    return num
display(9)   # pass any number to check whether number is even or odd
Comment

Decorators in python

# decorator function to convert to lowercase
def lowercase_decorator(function):
   def wrapper():
       func = function()
       string_lowercase = func.lower()
       return string_lowercase
   return wrapper
# decorator function to split words
def splitter_decorator(function):
   def wrapper():
       func = function()
       string_split = func.split()
       return string_split
   return wrapper
@splitter_decorator # this is executed next
@lowercase_decorator # this is executed first
def hello():
   return 'Hello World'
hello()   # output => [ 'hello' , 'world' ]
Comment

examples of function decorators in Python

from functools import wraps

def logit(logfile='out.log'):
    def logging_decorator(func):
        @wraps(func)
        def wrapped_function(*args, **kwargs):
            log_string = func.__name__ + " was called"
            print(log_string)
            # Open the logfile and append
            with open(logfile, 'a') as opened_file:
                # Now we log to the specified logfile
                opened_file.write(log_string + '
')
            return func(*args, **kwargs)
        return wrapped_function
    return logging_decorator

@logit()
def myfunc1():
    pass

myfunc1()
# Output: myfunc1 was called
# A file called out.log now exists, with the above string

@logit(logfile='func2.log')
def myfunc2():
    pass

myfunc2()
# Output: myfunc2 was called
# A file called func2.log now exists, with the above string
Comment

Decorators in Python

def first(msg):
    print(msg)


first("Hello")

second = first
second("Hello")
Comment

PREVIOUS NEXT
Code Example
Python :: poision in chinese 
Python :: pydrive download file 
Python :: python sum whole matrix comand 
Python :: duur wordt voor woorden kennis 
Python :: method for format age in python 
Python :: provide a script that prints the sum of every even numbers in the range [0; 100]. 
Python :: re.add python 
Python :: cv2 open blank window 
Python :: la commande pop python 
Python :: np.linalg.eigvals positive check python 
Python :: k means image classification 
Python :: matplotlib x tlabels ax.set_xlabel 
Python :: csv logger keras 
Python :: python measure volum from audio file 
Python :: pylatex add section without numbering 
Python :: sidetable github 
Python :: mute button tkinter 
Python :: Compute the mean of this RDD’s elements. 
Python :: somma array python 
Python :: dict from group pandas 
Python :: importando todo o pacote em python 
Python :: vitalik buterin age 
Python :: if query empty print python 
Python :: remove grid from 3d plots 
Python :: adding bootstrap grid dynamically django 
Python :: multiplication table with three lines of code in python 
Python :: replace special from beginning of string 
Python :: generic rectangle 
Python :: genskill bootcamp amazing python program 
Python :: first and last upper 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =