简体   繁体   中英

Is this a function decorator?

I've just started playing around with pyglet.

In the first demo , I ran accross code like this:

window = pyglet.window.Window()

@window.event
def on_draw():
    window.clear()
    label.draw()

I understand that this registers an event handler, but I don't understand how.

How could this be rewritten without the '@' syntax?

It's called an "event decorator." Yes, you could just write

window.on_draw = on_draw

after the "def on_draw()" definition, without using the decorator; but then if the window already had an on_draw, it would be overwritten. The decorator will "chain" multiple event handlers together.

According to the docs , Window.event is a decorator. Decorators can be simply added via using an @ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM