Re: I want to lirn pithon but pithon doesn't want me to do so

@45
The really short version is that:

@decorator(decorator_args)
def func():
    stuf

is shorthand for:

def func():
    stuff
stuff = decorator(decorator_args)(stuff)

And if you don't have any arguments you can just leave the parens off.

The longer but still short version is that functions in Python can return functions, it turns out that's useful if you want to take a function and modify what it does, and so Python added a shorthand.  But I'm guessing that you're moving from BGT or PB, which don't have any sort of closures at all, so going straight to decorators from that is kind of a big jump.

But an example decorator:

def print_when_called():
    def decorator(f):
        def inner(*args, **kwargs):
            print("I was called!")
            f(*args, **kwargs)
        return inner
    return decorator

@print_when_called
def g():
    print("After the decorator")

Which should be right but I didn't test it and I've only ever written a decorator 5 or so times ever, it's not something that comes up often from the making one side, just from the using it side.  The thing with args and kwargs is just how you make a function in Python which takes any number of arguments and forwards them on somewhere else.

So for example people will do this for logging like above (but with the actual function name, I forget how to get it though), or the @property decorator which does magic to make it possible for myobj.x = 5 to do something instead of just storing to x on myobj.  But decorators can also just return the function they were passed, and that's what some of the UI stuff and parts of NVDA do, since you still get a chance to for example register the function somewhere else.

It is really really hard to walk through those code examples in words is the problem.  Some things just aren't English friendly.  Me and the explanations you've found aren't being difficult to be difficult, it's because normal human language doesn't have the ability to talk about this.  Eventually you just get closures, and after that it's just meh whatever, but until then it will make no sense.  If you want something simpler with functions returning functions that you have a chance of following:

def print_something_specific(x):
    def ret():
        print(x)
    return ret

print_1 = print_something_specific(1)
print_2 = print_something_specific(2)
print_3 = print_something_specific(3)
print_foo = print_something_specific("foo")

Then print_1() prints 1, print_2() prints 2, print_foo() prints foo.  print_something_specific is a function that returns functions, just like you might write a function to return a string or a number.  This is basically the hello world of closures. Once you understand it you can probably understand the bigger thing that is decorators.

Another way to look at this though, is that decorators exist to let library writers make your life easy and so you don't need to understand, just use.

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

Reply via email to