Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: Decorators not worth the effort

Reply
Thread Tools

RE: Decorators not worth the effort

 
 
Prasad, Ramit
Guest
Posts: n/a
 
      09-14-2012
Jean-Michel Pichavant wrote:

[snip]


> Ultimately, thegoal is to have something like
>
> @timeout(2)
> def doAction1
>
> @timeout(4)
> def doAction2


[snip]


> Here's Steven example:
>
> # Untested!
> def timeout(t=15):
> # Decorator factory. Return a decorator to actually do the work.
> if FPGA:
> t *= 3
> def decorator(func):
> @functools.wraps(func)
> def inner(self, timeout):
> self.sendCmd("bootMe", timeout=t)
> return inner
> return decorator
>
> I can assure you, that for some python users, it's is not easy to understand
> what it does, this function returning a function which returns another
> (wrapped) function. It requires some effort.
>


I think it would help if it was renamed to set_timeout. And Iwould
not expect the Python user to need to understand how it *works*, just
to recognize what it *does* when it is used. I may not understand list's
sort method internals (beyond the use of timsort), but I know how to
use it to sort a list as I want. That is usually all I need.


For example, your colleagues just need to understand that the below
decorator is setting a timeout for the function.

@set_timeout(min=15)
def some_function():
'''blah'''
<code>


One minor note, the style of decorator you are using loses the docstring
(at least) of the original function. I would add the @functools.wraps(func)
decorator insideyour decorator.

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchaseor sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Decorators not worth the effort Jean-Michel Pichavant Python 4 09-15-2012 01:13 AM
Re: Decorators not worth the effort Chris Angelico Python 2 09-14-2012 04:37 PM
Re: Decorators not worth the effort Jean-Michel Pichavant Python 0 09-14-2012 03:35 PM
Re: Decorators not worth the effort Jean-Michel Pichavant Python 1 09-14-2012 03:19 PM
Re: Decorators not worth the effort andrea crotti Python 0 09-14-2012 02:12 PM



Advertisments