Skip to content

Commit 26a698b

Browse files
feat: partial_function - Return a new partial object which when called will behave like func called with the positional arguments args and keyword arguments keywords
1 parent 2e1e41f commit 26a698b

File tree

6 files changed

+10
-0
lines changed

6 files changed

+10
-0
lines changed

ebook/chapters/third_party_chapters/timing.tex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,7 @@ \subsection{range VS repeat}
5555
"Range" spend time: 0.01480103
5656
"Repeat" spend time: 0.01320004
5757
\end{lstlisting}
58+
59+
If you want to avoid passing the \lstinline{None} argument, you can create a new function called \lstinline{times}, which hides the \lstinline{None} argument.
60+
61+
\lstinputlisting[caption=partial\_function.py]{../standard_lib/partial_function.py}

ebook/python-snippets.epub

143 Bytes
Binary file not shown.

ebook/python-snippets.mobi

62 Bytes
Binary file not shown.

ebook/python-snippets.pdf

1.54 KB
Binary file not shown.

standard_lib/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ A collection of useful snippets using only the standard library.
3939
| open_browser_tab | Opens a new tab in a specified browser (and more) |
4040
| overwrite_dictionary | PEP 448 - overwriting dictionary of default values |
4141
| parse_query_string | Parse a query string |
42+
| partial_function | Return a new partial object which when called will behave like func called with the positional arguments args and keyword arguments keywords |
4243
| pass_multiple_dicts | Unpack multiple dicts and pass them as keyword args to a function |
4344
| pathlib_relpath | `pathlib.Path` implementation of `os.relpath` (should never be used) |
4445
| port_scanner | Simple port scanner implementation using built-in packages |

standard_lib/partial_function.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from functools import partial
2+
from itertools import repeat
3+
4+
times = partial(repeat, None)
5+
min(random.random() for _ in times(100_000))

0 commit comments

Comments
 (0)