Re: Earwax

@103 looking at your examples of how decorators work, I dont think it is entirely true. reffering to your example:
@decorator
def f():
    pass
is the same as:
def f():
    pass
decorator(f)

In your example, the two cases are the same if and only if the decorator doesn't transform the function in any way. i.e. if the only thing the decorator does is perform a side effect like adding the function to a some dictionary, prints something, or fires a missile.

the function needs to be updated with the result from the decorator in order for it to also be transformed. so the second case would actually need to be:
def f():
    pass
f = decorator(f)

here is my python session where I tested this:
>>> def dec(func):
...  print('side effect')
...  def newFunc(*args, **kvargs): return func(*args, **kvargs) + 1
...  return newFunc
...
>>> def f(): return 1
...
>>> f()
1
>>> dec(f)
side effect
<function dec.<locals>.newFunc at 0x000002113ADE7EA0>
>>> f()
1
>>> @dec
... def g(): return 1
...
side effect
>>> g()
2
>>> f = dec(f)
side effect
>>> f()
2

edit: because of this, your timer decorator example also doesnt work, because it doesn't actualy return inner which is defined. so it has no effect other than setting any function the decorator is used on to None. I know this is almost definitely just a mistake and you understand decorators, but just wanted to point out the problems with those examples in case a newbie would want to refer to them wink

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : chrisnorman7 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Dragonlee via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Dragonlee via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : chrisnorman7 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : chrisnorman7 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : chrisnorman7 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : chrisnorman7 via Audiogames-reflector

Reply via email to