python function decorator

def wrapper(func): def wrapped_func(*args,**kwargs): # do something before func print 'wrapping: '+func.__name__ func(*args,**kwargs) #do something after func print 'finished wrapping' return wrapped_func #To decorate a function simply use @ and the name of the decorator function @wrapper def some_function(param): #Actual code here print 'some_function! (' + str(param) + ')' if __name__ == '__main__': some_function('test')
How to decorate a function in python

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.