Python Tip #275 (of 365): Pre-fill arguments with functools.partial. >>> from fu...
Trey Hunner ๐ @treyhunner@mastodon.social
Python Tip #275 (of 365):
Pre-fill arguments with functools.partial.
>>> from functools import partial
>>> hex_to_int = partial(int, base=16)
>>> hex_to_int("CAFE")
51966
This is sometimes called "partially evaluating" a function.
I prefer this:
hex_to_int = partial(int, base=16)
Instead of this:
hex_to_int = lambda s: int(s, base=16)
partial is more specialized than "lambda": its only purpose is to call a callable with certain arguments preset.
๐งต (1/3)