Created
March 23, 2010 23:35
-
-
Save exogen/341816 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import inspect | |
class Meta(type): | |
def __init__(cls, name, bases, attrs): | |
for attr, value in attrs.items(): | |
if inspect.isfunction(value): | |
setattr(cls, attr, decorate(value)) | |
for base in bases: | |
for attr in dir(base): | |
value = getattr(base, attr) | |
if inspect.ismethod(value): | |
setattr(cls, attr, decorate(value)) | |
def decorate(func): | |
# Check the flag to prevent double decoration. | |
if getattr(func, '_decorated', False): | |
return func | |
def decorated(instance, *args, **kwargs): | |
return func(instance, *args, **kwargs) | |
# Set the flag to prevent double decoration. | |
decorated._decorated = True | |
return decorated |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment