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 @
.
精彩评论