Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-119127: functools.partial placeholders#119827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
ee7333c8bcc462c67c9b4680d9009591ff5067e9388af20b3607a0b1f55801e58941453722e07a79c2af12aaa7292c767b496a9d238d9c11707b95714b38ca32bca198576493a3fd2d608529936fea348caec6e8115b8c53f5f00b202c9292c16d38400ff558ccc38fe7c82c7c9b7ef3e59d7117bfc5917957a978aaee6afe8e0ad00dd80ed352cfa9038ed549b8c71bc1fdbd30672211185510266b4fadd58a125971fbb9033650d31e5d1a3d39b09e4c5df16f12f882dd600f9cb653d255524404044e800217b38ee45011f47db3c872bdfd16189a6c6ef21c8d73ea8bd3ae70e47ed2eacf5ef78d8d30a8640e6e3d28266c305d14bf68cee642d58d6c28e8744bcbb8964704881ae6c3ad7d95e5d484File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -330,7 +330,7 @@ The :mod:`functools` module defines the following functions: | ||
| .. function:: partial(func, /, *args, **keywords) | ||
| Return a new :ref:`partial object<partial-objects>` which when called | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| will behave like *func* called with the positional arguments *args* | ||
| and keyword arguments *keywords*. If more arguments are supplied to the | ||
| call, they are appended to *args*. If additional keyword arguments are | ||
| @@ -380,7 +380,8 @@ The :mod:`functools` module defines the following functions: | ||
| If :data:`Placeholder` sentinels are present in *args*, they will be filled first | ||
| when :func:`partial` is called. This allows custom selection of positional arguments | ||
| to be pre-filled when constructing a :ref:`partial object <partial-objects>`. | ||
| If :data:`Placeholder` sentinels are used, all of them must be filled at call time: | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| >>> from functools import partial, Placeholder | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page.
picnixz marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| @@ -390,16 +391,16 @@ The :mod:`functools` module defines the following functions: | ||
| >>> say_to_world('Hello', 'dear') | ||
| Hello dear world! | ||
| Calling ``say_to_world('Hello')`` wouldraise a :exc:`TypeError`. | ||
| .. versionchanged:: 3.14 | ||
| Support for :data:`Placeholder` in *args* | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| .. data:: Placeholder | ||
| Asingleton object used as a sentinelto represent a | ||
| "gap" in positional arguments when calling :func:`partial`. | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| This objecthas featuressimilar to ``None``: | ||
| >>> type(Placeholder)() is Placeholder | ||
| True | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -281,12 +281,12 @@ class PlaceholderType: | ||
| The type of the Placeholder singleton. | ||
| Used as a placeholder for partial arguments. | ||
| """ | ||
| __instance = None | ||
dg-pb marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| def __new__(cls): | ||
| if cls.__instance is None: | ||
| cls.__instance = object.__new__(cls) | ||
| return cls.__instance | ||
| def __repr__(self): | ||
| return 'Placeholder' | ||
| @@ -313,13 +313,13 @@ class partial: | ||
| def __new__(cls, func, /, *args, **keywords): | ||
| if not callable(func): | ||
| raise TypeError("the first argument must be callable") | ||
| np = 0 | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| nargs = len(args) | ||
| if args: | ||
| while nargs and args[nargs -1] is Placeholder: | ||
| nargs -= 1 | ||
| args = args[:nargs] | ||
| np = args.count(Placeholder) | ||
dg-pb marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| if isinstance(func, partial): | ||
| pargs = func.args | ||
| pnp = func.placeholder_count | ||
| @@ -335,10 +335,10 @@ def __new__(cls, func, /, *args, **keywords): | ||
| pos += 1 | ||
| if pnp < nargs: | ||
| all_args.extend(args[pnp:]) | ||
| np += pnp - end | ||
| args = tuple(all_args) | ||
| else: | ||
| np += pnp | ||
| args = func.args + args | ||
| keywords = {**func.keywords, **keywords} | ||
| func = func.func | ||
| @@ -347,26 +347,28 @@ def __new__(cls, func, /, *args, **keywords): | ||
| self.func = func | ||
| self.args = args | ||
| self.keywords = keywords | ||
| self.placeholder_count =np | ||
| return self | ||
| def __call__(self, /, *args, **keywords): | ||
| np = self.placeholder_count | ||
| p_args = self.args | ||
| if np: | ||
| if len(args) < np: | ||
| raise TypeError( | ||
| "missing positional arguments " | ||
| "in 'partial' call; expected " | ||
| f"at least {np}, got {len(args)}") | ||
| p_args = list(p_args) | ||
| j, pos = 0, 0 | ||
| while j <np: | ||
| pos =p_args.index(Placeholder, pos) | ||
| p_args[pos] = args[j] | ||
| j += 1 | ||
| pos += 1 | ||
| args =args[j:] | ||
| keywords = {**self.keywords, **keywords} | ||
| return self.func(*p_args, *args, **keywords) | ||
| @recursive_repr() | ||
| def __repr__(self): | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| Positional argumentsof:func:`functools.partial` objects | ||
| now support placeholders via :data:`functools.Placeholder`. |