RE: Decorators not worth the effort

Discussion in 'Python' started by Prasad, Ramit, Sep 15, 2012.

  1. 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.
    Prasad, Ramit, Sep 15, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jean-Michel Pichavant

    Re: Decorators not worth the effort

    Jean-Michel Pichavant, Sep 14, 2012, in forum: Python
    Replies:
    4
    Views:
    192
    Steve Howell
    Sep 15, 2012
  2. Jean-Michel Pichavant

    Re: Decorators not worth the effort

    Jean-Michel Pichavant, Sep 14, 2012, in forum: Python
    Replies:
    1
    Views:
    136
    Steven D'Aprano
    Sep 14, 2012
  3. andrea crotti

    Re: Decorators not worth the effort

    andrea crotti, Sep 14, 2012, in forum: Python
    Replies:
    0
    Views:
    134
    andrea crotti
    Sep 14, 2012
  4. Chris Angelico

    Re: Decorators not worth the effort

    Chris Angelico, Sep 14, 2012, in forum: Python
    Replies:
    2
    Views:
    156
    88888 Dihedral
    Sep 14, 2012
  5. Jean-Michel Pichavant

    Re: Decorators not worth the effort

    Jean-Michel Pichavant, Sep 14, 2012, in forum: Python
    Replies:
    0
    Views:
    131
    Jean-Michel Pichavant
    Sep 14, 2012
Loading...

Share This Page