Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong><code>&lt;edit&gt;</code></strong> I just realized I misread the question and that you are not trying to get function attributes, but local variable values from the function. What you want to do is not possible because those local variables are not created until the function is run, and the local scope for the function is deleted as soon as the function returns or raises an exception.</p> <p>I am leaving my original answer because you could potentially rewrite your function to use attributes instead of local variables and still use this decorator to effectively do what you want.</p> <p>It would be helpful for you to post what you have currently attempted and some example calls with expected output if it was working correctly.<strong><code>&lt;/edit&gt;</code></strong></p> <p>When you need a function with attributes, it is generally a good idea to use a callable class instead of a normal function definition.</p> <p>Here is an example of a decorator where the wrapper is a callable class, which allows the decorator to access variables easily, because they are instance variables of the wrapper class:</p> <pre><code>def deco(func): class Wrapper(object): def __init__(self): self.foo = None def __call__(self, *args): print 'old foo:', self.foo result = func(*args) print 'new foo:', self.foo return result return Wrapper() @deco def my_func(new_foo): my_func.foo = new_foo </code></pre> <p>Which results in <code>my_func</code> behaving like this:</p> <pre><code>&gt;&gt;&gt; my_func('test') old foo: None new foo: test &gt;&gt;&gt; my_func.foo 'test' &gt;&gt;&gt; my_func(42) old foo: test new foo: 42 &gt;&gt;&gt; my_func.foo 42 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload