Apr 28, 2008 10:30
a couple fun tricks in python I've discovered recently:
Change the arguments a function accepts with a decorator:
>>> def fillin(func):
... def ret():
... func("hi")
... return ret
...
>>> @fillin
... def a(arg):
... print arg
...
>>> a()
hi
>>>
If you want to be a real jerk about it, you can define a function which defines and returns a decorator, which in turn defines a function which calls the original function, substituting the parameter passed into the _decorator at definition_. I can't think of any good reason to do this, but it's sort of neat.
>>> def fillin(arg):
... def decor(func):
... def ret():
... func(arg)
... return ret
... return decor
...
>>> @fillin("hi")
... def a(arg):
... print arg
...
>>> a()
hi