Python's hidden convenience module, functools
Especially the functools.partial function is messed up.
It's hard to explain if you can say that it returns a function with some arguments fixed.
sample.py
from functools import partial
def sayhello(message=u"hello", to=u"ryo"):
print(u"{1}Mr.{0}".format(message, to))
def main():
sayhello()
konnnichiwa = partial(sayhello, u"Hello")
konnnichiwa("nishizawa")
konnnichiwa("takahashi")
if __name__ == "__main__":
main()
When you run
> python sample.py
ryo, hello
Hello nishizawa
Hello takahashi
It will be.
Useful when defeating PySide callback functions, etc.
Recommended Posts