Francesco Bochicchio wrote:
def dec_f(f):
    def inner_f():
        if f.enabled:
           f()
    return inner_f

@dec_f
def funct():
    print "Ciao"
The three lines above should behave a lot like:
    def funct_original():
        print "Ciao"
    funct = dec_f(funct_original)

> funct.enabled = True
> funct()
This is decorating "funct", but inner_f is testing the argument it was
passed (funct_original), not inner_f (which is now called funct).  No
wonder it finds no "enabled" attribute.


> So I have a workaround, but still don't understant why the original
> code does not work.
> Anyone can point me to an explanation?

So, you can fix this like so:
    def dec_f(f):
        def inner_f():
            if inner_f.enabled:
                f()
        return inner_f

Or even:
    def dec_f(f):
        def inner_f():
            if inner_f.enabled:
                f()
        inner_f.enabled = False
        return inner_f

Thus testing the thing that you were marking with funct.enabled = True.

Does this help explain?

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to