[issue42646] Add function that supports "applying" methods

wyz23x2 report at bugs.python.org
Tue Dec 15 08:07:08 EST 2020


New submission from wyz23x2 <wyz23x2 at 163.com>:

Doing this is generally very annoying:
y = x.copy()
y.some_method()
Sometimes x doesn't have copy(), so:
from copy import deepcopy
y = deepcopy(x)
y.some_method()

So maybe a function could be added to help.
For example:

def apply(obj, function, /, args=(), kwargs={}):
    try:
        new = obj.copy()
    except AttributeError:
        from copy import copy
        new = copy(obj)
    function(new, *args, **kwargs)
    return new
# implement reversed() for list
lis = [1, 2, 3, 4, 5]
arr = apply(lis, list.reverse)
print(arr)  # [5, 4, 3, 2, 1]

apply() maybe isn't the best name because of the builtin apply() in Python 2, but that's EOL. It could be added in the standard library.

----------
components: Library (Lib)
messages: 383050
nosy: wyz23x2
priority: normal
severity: normal
status: open
title: Add function that supports "applying" methods
versions: Python 3.10

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42646>
_______________________________________


More information about the Python-bugs-list mailing list