making a variable available in a function from decorator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rkmr.em@gmail.com

    making a variable available in a function from decorator

    Hi
    I create a variable in a decorator. i want to be able to access that
    variable in the function to be decorated. How to do this?
    thanks
  • Marc 'BlackJack' Rintsch

    #2
    Re: making a variable available in a function from decorator

    On Sun, 29 Jul 2007 15:22:47 -0700, rkmr.em@gmail.c om wrote:
    I create a variable in a decorator. i want to be able to access that
    variable in the function to be decorated. How to do this?
    Pass it as argument to the function:

    def deco(func):
    eggs = 42
    def decorated(*args , **kwargs):
    kwargs['spam'] = eggs
    func(*args, **kwargs)
    return decorated

    @deco
    def test(parrot, spam):
    print parrot, spam

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • rkmr.em@gmail.com

      #3
      Re: making a variable available in a function from decorator

      is it possible to do this without passing it as a function argument?

      On 30 Jul 2007 06:17:25 GMT, Marc 'BlackJack' Rintsch <bj_666@gmx.net wrote:
      On Sun, 29 Jul 2007 15:22:47 -0700, rkmr.em@gmail.c om wrote:
      >
      I create a variable in a decorator. i want to be able to access that
      variable in the function to be decorated. How to do this?
      >
      Pass it as argument to the function:
      >
      def deco(func):
      eggs = 42
      def decorated(*args , **kwargs):
      kwargs['spam'] = eggs
      func(*args, **kwargs)
      return decorated
      >
      @deco
      def test(parrot, spam):
      print parrot, spam
      >
      Ciao,
      Marc 'BlackJack' Rintsch
      --

      >

      Comment

      • Bruno Desthuilliers

        #4
        Re: making a variable available in a function from decorator

        rkmr.em@gmail.c om a écrit :
        (top-post corrected)
        >
        On 30 Jul 2007 06:17:25 GMT, Marc 'BlackJack' Rintsch <bj_666@gmx.net wrote:
        >On Sun, 29 Jul 2007 15:22:47 -0700, rkmr.em@gmail.c om wrote:
        >>
        >>I create a variable in a decorator. i want to be able to access that
        >>variable in the function to be decorated. How to do this?
        >
        >Pass it as argument to the function:
        >>
        >def deco(func):
        > eggs = 42
        > def decorated(*args , **kwargs):
        > kwargs['spam'] = eggs
        > func(*args, **kwargs)
        > return decorated
        >>
        >@deco
        >def test(parrot, spam):
        > print parrot, spam
        is it possible to do this without passing it as a function argument?
        What's your use case, exactly ? Having a function depending on a name
        being set by a decorator is not exactly pythonic, and arguments are
        meant to pass variables to functions...


        Comment

        • Evan Klitzke

          #5
          Re: making a variable available in a function from decorator

          On 7/30/07, rkmr.em@gmail.c om <rkmr.em@gmail. comwrote:
          is it possible to do this without passing it as a function argument?
          >
          Sort of. Functions are objects in python, so you can set attribute on them. E.g.

          def foo():
          return foo.c

          foo.c = 1
          print foo()

          Which will print 1. Of course, it would generally be better to write
          your own class for this sort of thing, so that you can set the
          variable in the instance scope.

          --
          Evan Klitzke <evan@yelp.co m>

          Comment

          • Bruno Desthuilliers

            #6
            Re: making a variable available in a function from decorator

            Evan Klitzke a écrit :
            On 7/30/07, rkmr.em@gmail.c om <rkmr.em@gmail. comwrote:
            >
            >>is it possible to do this without passing it as a function argument?
            >>
            Sort of. Functions are objects in python, so you can set attribute on them. E.g.
            >
            def foo():
            return foo.c
            >
            foo.c = 1
            print foo()
            >>def foo():
            .... print foo.c
            ....
            >>foo.c = 1
            >>bar = foo
            >>del foo
            >>bar()
            Traceback (most recent call last):
            File "<stdin>", line 1, in ?
            File "<stdin>", line 2, in foo
            NameError: global name 'foo' is not defined
            >>>
            Which will print 1. Of course, it would generally be better to write
            your own class for this sort of thing, so that you can set the
            variable in the instance scope.
            Indeed. But even with OO, explicit is better than implicit.

            Comment

            Working...