gpt4 book ai didi

python - 为类中的方法实现就地操作

转载 作者:行者123 更新时间:2023-12-03 16:48:18 24 4
gpt4 key购买 nike

pandas许多方法都有关键字参数 inplace .这意味着如果 inplace=True ,被调用的函数将在对象本身上执行,并返回 None,另一方面,如果 inplace=False原始对象将保持不变,并在返回的新实例上执行该方法。我已经成功地实现了这个功能,如下所示:

from copy import copy

class Dummy:
def __init__(self, x: int):
self.x = x

def increment_by(self, increment: int, inplace=True):
if inplace:
self.x += increment
else:
obj = copy(self)
obj.increment_by(increment=increment, inplace=True)
return obj

def __copy__(self):
cls = self.__class__
klass = cls.__new__(cls)
klass.__dict__.update(self.__dict__)
return klass

if __name__ == "__main__":
a = Dummy(1)
a.increment_by(1)
assert a.x == 2
b = a.increment_by(2, inplace=False)
assert a.x == 2
assert b.x == 4
它按预期工作。但是,我有很多方法可以重复相同的模板:
def function(self, inplace=True, **kwds)
if inplace:
# do something
else:
obj = copy(self)
obj.function(inplace=True, *args, **kwds)
return obj
为了避免重复,我想创建一个装饰器和标记函数,它们可以就地执行,也可以非就地执行。
我想这样使用它
from copy import copy

class Dummy:
def __init__(self, x: int):
self.x = x

@inplacify
def increment_by(self, increment: int):
self.x += increment # just the regular inplace way

def __copy__(self):
cls = self.__class__
klass = cls.__new__(cls)
klass.__dict__.update(self.__dict__)
return klass
我希望它的行为与上面的示例相同。
我试过写不同的装饰器
(像这样开始的东西
def inplacify(method):
def inner(self, *method_args, **method_kwds):
inplace = method_kwds.pop("inplace", True)
def new_method(inplace, *method_args, **method_kwds):
)
但我每次都卡住了。我需要 self 的引用为了返回我没有的类(class)副本。使用装饰器更改函数签名也感觉有点模糊。我有几个问题:这个行为可以实现吗?我需要一个类装饰器吗?这是否被认为是一种不好的做法,如果是这样,处理此类问题的最佳选择是什么?

最佳答案

如果你的方法有 return self ,以下工作:

import copy

def inplacify(method):
def wrap(self,*a,**k):
inplace = k.pop("inplace",True)
if inplace:
method(self,*a,**k)
else:
return method(copy.copy(self),*a,**k)
return wrap

class classy:
def __init__(self,n):
self.n=n

@inplacify
def func(self,val):
self.n+=val
return self
我测试了一下:
inst = classy(5)
print(inst.n)
inst.func(4)
print(inst.n)
obj = inst.func(3,inplace=False)
print(inst.n,obj.n)
并得到了预期的结果:
5
9
9 12
希望这能满足您的需求。

关于python - 为类中的方法实现就地操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63207734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com