in Technology by
What are decorators in Python?

1 Answer

0 votes
by

Decorators in Python are essentially functions that add functionality to an existing function in Python without changing the structure of the function itself. They are represented by the @decorator_name in Python and are called in bottom-up fashion. For example:

# 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' ]

The beauty of the decorators lies in the fact that besides adding functionality to the output of the method, they can even accept arguments for functions and can further modify those arguments before passing it to the function itself. The inner nested function, i.e. 'wrapper' function, plays a significant role here. It is implemented to enforce encapsulation and thus, keep itself hidden from the global scope.

<pre class="skill" style='box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; padding: 9.5px; border: none; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; -webkit-font-smoothing: subpixel-antialiased; text-rendering: optimizelegibility; font-size: 13px; vertical-al

Related questions

0 votes
    Can someone tell me how to make two decorators that would do the following in python: @makeitalic @makebold def say( ... to make HTML Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    What are the values of the following Python expressions? 2**(3**2) (2**3)**2 2**3**2 a) 512, 64, 512 b) 512, 512, 512 c) 64, 512, 64 d) 64, 64, 64...
asked Dec 31, 2022 in Technology by JackTerrance
0 votes
    What are builtin Data Types in Python?...
asked Aug 20, 2021 in Technology by JackTerrance
0 votes
    What are the supported data types in Python?...
asked Apr 24, 2021 in Technology by JackTerrance
0 votes
    What are the Difference between staticmethod and classmethod in Python?...
asked Jan 9, 2021 in Technology by JackTerrance
0 votes
    What are metaclasses in Python?...
asked Jan 9, 2021 in Technology by JackTerrance
0 votes
0 votes
    How are arguments passed by value or by reference in python?...
asked Dec 7, 2020 in Technology by JackTerrance
0 votes
0 votes
    What are generators in Python?...
asked Dec 6, 2020 in Technology by JackTerrance
0 votes
    What are global, protected and private attributes in Python?...
asked Dec 6, 2020 in Technology by JackTerrance
0 votes
    What are modules and packages in Python?...
asked Dec 6, 2020 in Technology by JackTerrance
0 votes
    What are the common built-in data types in Python?...
asked Dec 6, 2020 in Technology by JackTerrance
0 votes
    What are Python namespaces? Why are they used?...
asked Dec 6, 2020 in Technology by JackTerrance
0 votes
    What are the benefits of using Python?...
asked Dec 6, 2020 in Technology by JackTerrance
...