Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #68573
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: Decorator |
| Date | 2014-03-20 10:59 +0100 |
| Organization | None |
| References | <CACk27ULPKOaYzU404yanq7e=4qZMhBgk24LwmVZJ=gZz51wQJQ@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.8294.1395309599.18130.python-list@python.org> (permalink) |
muru kessan wrote:
> Is there a difference between accessing decorators via '@' symbol and
> hard coding that ? esp when the function passed to the decorator is a
> recursive one?
The difference is not the decorator but the recursive function call.
Consider
Case 1:
@deco
def f():
...
f() # calls the decorated function
...
f()
Case 2:
def f()
...
f() # calls the undecorated function
...
g = deco(f)
g()
The function call f() will invoke whatever the global name f is bound to a
the time of invocation. So
Case 3:
def f()
...
f() # calls the decorated function
...
f = deco(f)
f()
In your code change
fib1 = isOddMy(fib)
to
fib = isOddMy(fib)
and the without@ version will produce the same output as the with@ version.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Decorator Peter Otten <__peter__@web.de> - 2014-03-20 10:59 +0100
csiph-web