create account

DECORATORS IN PYTHON PROGRAMMING by leoumesh

View this thread on: hive.blogpeakd.comecency.com
· @leoumesh ·
$4.58
DECORATORS IN PYTHON PROGRAMMING
Decorators is a concept that belongs to the Functional Programming and you can't find this concept in procedural programming like C/C++ or Object Oriented Programming like Java, Php. Decorators allow you to use another function inside a function and you can use that function to perform different tasks without changing the original function code. Simple understand this concept as "Using Function Inside a Function" Here's a simple program in python which demonstrates the concept of decorators.

```
def higher_function():
    def lower_function():
        print("How you doing")
    lower_function()
 
higher_function()
```

You can see the output as:

<center>
![Screenshot_2.png](https://images.hive.blog/DQmTMZBvJcUigf9nCunhXgoMoEmbmDNiYqvVmeMqJh26ito/Screenshot_2.png)
</center>

Now let's see the working of decorators. We would create a function that subtracts two values like if we passed (3,5) then it will return -2 as output but remember we want to subtract 3 from 5 and returns output as 2 (not negative). Then in this case we use decorators to modify existing function class that subtracts two values.

```
def subtraction(x,y):
    print(x-y)
    
def sub(func):
    def operation(x,y):
        if x<y:
            x,y=y,x
        return func(x,y)
    return operation

subtraction=sub(subtraction)

subtraction(5,10)
```

You can see the output as follows:
<center>
![Screenshot_3.png](https://images.hive.blog/DQmP7QUpfwS8V4CxbfDjnKcuu5iZQYUNWqZ5Rg6CpJokZSi/Screenshot_3.png)
</center>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 36 others
properties (23)
authorleoumesh
permlinkdecorators-in-python-programming
categoryhive-174578
json_metadata{"tags":["hive-174578"],"image":["https://images.hive.blog/DQmTMZBvJcUigf9nCunhXgoMoEmbmDNiYqvVmeMqJh26ito/Screenshot_2.png","https://images.hive.blog/DQmP7QUpfwS8V4CxbfDjnKcuu5iZQYUNWqZ5Rg6CpJokZSi/Screenshot_3.png"],"app":"hiveblog/0.1","format":"markdown"}
created2020-08-22 10:56:09
last_update2020-08-22 10:56:09
depth0
children0
last_payout2020-08-29 10:56:09
cashout_time1969-12-31 23:59:59
total_payout_value2.776 HBD
curator_payout_value1.800 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,503
author_reputation212,340,493,251,438
root_title"DECORATORS IN PYTHON PROGRAMMING"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,208,585
net_rshares14,842,024,772,908
author_curate_reward""
vote details (100)