2👍
Actually, these both are exactly the same:
def foo(*args):
pass
foo = deco(foo)
@deco
def foo(*args):
pass
If you want to decorate bar
and call it foo
, foo = deco(bar)
is the right way. It says: “decorate this previously defined thing called bar
and call it foo
“. The point of the decorator syntax is to state the wrapping function before the definition, not to rename it.
Unless you need to use bar
later, there is no reason to call the undecorated function with a different name. By doing this you lose precisely the ability to use the decorator syntax sugar.
deco
doesn’t need to be a function. It can be an object with a __call__
method, which is useful precisely to encapsulate side effects.
0👍
Your examples do not express the same things in every case! Why do you insist on using bar?
Take your first example:
#Option 1
def bar(*args):
pass
foo = deco(bar)
#Option2
@deco
def foo(*args):
pass
Option 1 does (literally)
foo = deco(bar)
but Option 2 is the equivalent of
foo = deco(foo)
Can’t you see the difference there?
So, in short, yes: your assumption and your expectations are wrong.
If you need the undecorated version of your function, as well as the decorated one, just save it beforehand:
def foo(*args):
pass
bar = foo
foo = deco(foo)