Last active
August 5, 2018 14:33
-
-
Save LouiS0616/8fd9ff823f05cc8b40b6853f5d44e0f5 to your computer and use it in GitHub Desktop.
This file contains 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
def printable_func(str_func=None): | |
if str_func is None: | |
str_func = lambda func: func.__name__ | |
def inner(func): | |
return type( | |
func.__name__, (object, ), | |
{ | |
**vars(func), | |
'__call__': | |
lambda self, *args, **kwargs: func(*args, **kwargs), | |
'__str__': | |
lambda self: str_func(func), | |
} | |
)() | |
return inner | |
def main(): | |
def boring_spam(): | |
return 'spam' | |
@printable_func() | |
def printable_spam(): | |
return 'spam' | |
print(boring_spam()) # spam | |
print(boring_spam) # <function boring_spam at ...> | |
print(printable_spam()) # spam | |
print(printable_spam) # printable_spam | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment