+ 1

Can somebody explain decorators for me pliz in python

4th Jan 2017, 5:51 PM
@Rokeow
@Rokeow - avatar
2 Respostas
+ 2
Decorators are wrappers around functions, methods, and classes. Typically they add some functionality to the wrapped code but they can also be used to simply modify parameters or do some logging. Here's a simple example: import time def time_call(func): def _wrapper(*args, **kwargs): current_time = time.time() func(*args, **kwargs) elapsed_time = time.time() - current_time print('Elapsed time: {}'.format(elapsed_time)) @time_call def my_func(sec): time.sleep(sec) my_func(2) # prints 'Elapsed time: 2000' This is a simple example and there's way more you can do. This is a very powerful feature when used right.
4th Jan 2017, 6:19 PM
James Durand
James Durand - avatar
+ 2
Here is an example of what a decorator may be useful for: https://code.sololearn.com/cD09ijlIS6Ev/?ref=app
25th Mar 2018, 4:35 PM
Denys Yeromenko
Denys Yeromenko - avatar