[Tutor] basic decorator question
Peter Otten
__peter__ at web.de
Mon Jul 24 12:13:34 EDT 2017
bruce wrote:
> Hi.
>
> I've seen sites discuss decorators, as functions that "wrap" and
> return functions.
>
> But, I'm sooo confuzed! My real question though, can a decorator have
> multiple internal functions? All the examples I've seen so far have a
> single internal function.
>
> And, if a decorator can have multiple internal functions, how would
> the calling sequence work?
>
> But as a start, if you have pointers to any really "basic" step by
> step sites/examples I can look at, I'd appreciate it. I suspect I'm
> getting flumoxed by something simple.
Here's a completely useless decorator to drive home the point that
@deco
def f(...):
...
is only a fancy way to write
def f(...):
...
f = deco(f)
>>> def fortytwo(func):
... print("I don't care about your function")
... return 42
...
>>> @fortytwo
... def f():
... print("this is important")
...
I don't care about your function
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>> f
42
More information about the Tutor
mailing list