Decorators without function

Discussion in 'Python' started by Vedran, Aug 9, 2010.

  1. Vedran

    Vedran Guest

    Hello!

    Is it possible to apply a decorator on a block of code, without defining
    a function that decorator is applied to.

    I have to generate a lot of similar graphs. For that reason I use
    plot_decorator to perform usual figure setup(outfile, legend, x_label,
    y_label and so on), and pylab.plot commands to do the actual plotting.

    Current solution:

    @plot_decorator(fig_params1)
    def plt():
    pylab.plot(..first graph data...)
    pylab.plot(..second graph data..)
    plt()

    Is it possible to apply decorator only to the body of plt (several
    pylab.plot commands) without actually defining this function, because I
    use it only once, right after the definition?
    Vedran, Aug 9, 2010
    #1
    1. Advertising

  2. Vedran

    Peter Otten Guest

    Vedran wrote:

    > Hello!
    >
    > Is it possible to apply a decorator on a block of code, without defining
    > a function that decorator is applied to.


    You can only decorate functions or classes.

    > I have to generate a lot of similar graphs. For that reason I use
    > plot_decorator to perform usual figure setup(outfile, legend, x_label,
    > y_label and so on), and pylab.plot commands to do the actual plotting.
    >
    > Current solution:
    >
    > @plot_decorator(fig_params1)
    > def plt():
    > pylab.plot(..first graph data...)
    > pylab.plot(..second graph data..)
    > plt()
    >
    > Is it possible to apply decorator only to the body of plt (several
    > pylab.plot commands) without actually defining this function, because I
    > use it only once, right after the definition?


    I have a hunch that a contextmanager is a better match for your
    requirements, see

    http://docs.python.org/library/contextlib.html#contextlib.contextmanager

    Peter
    Peter Otten, Aug 9, 2010
    #2
    1. Advertising

  3. Vedran wrote:

    > @plot_decorator(fig_params1)
    > def plt():
    > pylab.plot(..first graph data...)
    > pylab.plot(..second graph data..)
    > plt()


    You could do something like

    def call(f):
    f()

    @call
    @plot_decorator(fig_params1)
    def plt():
    pylab.plot(..first graph data...)
    pylab.plot(..second graph data..)

    --
    Greg
    Gregory Ewing, Aug 10, 2010
    #3
    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. Arien Malec

    PEP 318 decorators are not Decorators

    Arien Malec, Aug 13, 2004, in forum: Python
    Replies:
    11
    Views:
    567
    Arien Malec
    Aug 16, 2004
  2. Porky Pig Jr
    Replies:
    0
    Views:
    264
    Porky Pig Jr
    Aug 14, 2004
  3. Bengt Richter
    Replies:
    0
    Views:
    291
    Bengt Richter
    Aug 25, 2004
  4. Replies:
    5
    Views:
    293
  5. Nick Donohue

    function decorators

    Nick Donohue, Sep 28, 2010, in forum: Python
    Replies:
    3
    Views:
    267
    Terry Reedy
    Sep 29, 2010
Loading...

Share This Page