Confused with Functions and decorators

Chris Angelico rosuav at gmail.com
Sat Jul 19 07:50:42 EDT 2014


On Sat, Jul 19, 2014 at 9:40 PM, Jerry lu <nicholascannon1 at gmail.com> wrote:
> oh yeah i forgot about the decorators. Um say that you wanted to decorate a function with the outer() func you would just put @outer on top of it? And this is the same as passing another func into the outer func?
>
> and also with the first example you say x is in the scope when is was created can you define x in the outer func and refer to it in the inner func?

Firstly, please don't use Google Groups, or if you must, please clean
up its messes. It's conventional on mailing lists and newsgroups to
quote a bit of text to give context; so, for instance, you mention one
of my examples, without quoting it.

Secondly: Don't worry about decorators. You almost never need them,
and they're very simple. (Python avoids magic whenever it can!) These
two are equivalent:

def inner():
    pass
inner = outer(inner)

@outer
def inner():
    pass

That's all the decorator does. It's that simple.

As to scoping... read up on closures. There's a lot that I could say,
but you'll find some excellent articles on the internet about it.
Also, you'll learn how to find information, which is a skill you are
going to need in this life :)

ChrisA



More information about the Python-list mailing list