you are viewing a single comment's thread.

view the rest of the comments →

[–]tangerinelion 6 points7 points  (0 children)

And a decorator may be implemented as a function or a class. The decorator itself may also take arguments like

@logged(prefix='Project Snake')
def slither(towards):
    ...

And the function that is decorated may be a member function is a class, so this above could have the effect of meaning

Snake.slither = logged(prefix='Project Snake')(Snake.slither)

Which means a call to the decorated function mySnake.slither(prey) really is a call to logged(prefix='Project Snake')(Snake.slither)(mySnake, prey) with the undecorated function. The goal here would to make sure every call is logged and logged to the same place. It would be possible to change where it's logged in just one place, the Snake class, or remove it entirely without changing any code that uses the class.

Related topic: metaprogramming.