Partial in case of trouble

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

Partial in case of trouble
[Bash / linux] Notes in case of trouble
Decoder description in veriloggen (sample description of case statement)
case class in python
Code that sets the default value in case of AttributeError
Handling of quotes in [bash]
List of nodes in diagrams
Equivalence of objects in Python
Implementation of quicksort in Python
Pixel manipulation of images in Python
The story of participating in AtCoder
Implementation of login function in Django
Division of timedelta in Python 2.7 series
Features of pd.NA in pandas 1.0.0 (rc0)
MySQL-automatic escape of parameters in python
Handling of JSON files in Python
Install GoLang in goenv of anyenv
Implementation of life game in Python
Waveform display of audio in Python
Summary of various operations in Tensorflow
Chain of Responsibility pattern in Java
The story of the "hole" in the file
The meaning of ".object" in Django
Law of large numbers in python
Implementation of original sorting in Python
Reversible scrambling of integers in Python
Convert PDF of new corona outbreak case in Aichi prefecture to CSV