如何创建python函数的副本

编程入门 行业动态 更新时间:2024-10-28 10:26:30
本文介绍了如何创建python函数的副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以创建python函数的真实副本?最明显的选择是 docs.python/2/library/copy.html 但是我读到了:

Is there a possibility to create real copies of python functions? The most obvious choice was docs.python/2/library/copy.html but there I read:

它通过 返回原始对象来复制函数和类(浅层和深层)不变;

It does "copy" functions and classes (shallow and deeply), by returning the original object unchanged;

我需要一个真实的副本,因为我可能会更改函数的某些属性。

I need a real copy, because I might change some attributes of the function.

更新:

我知道评论中提到的所有可能性。我的用例基于元编程,其中我根据一些声明性规范构造了类。完整的细节对于SO来说太长了,但是基本上我有一个类似

I'm aware of all the possibilities which are mentioned in the comments. My use case is based on meta programming where I construct classes out of some declarative specifications. Complete details would be too long for SO, but basically I have a function like

def do_something_usefull(self,arg): self.do_work()

我将向各种类添加此方法。这些类可以完全不相关。使用mixin类不是一种选择:我将拥有许多此类函数,最终将为每个函数添加一个基类。我当前的解决方法是将此功能包装在这样的工厂中:

I will add this method to various classes. Thoses classes can be completly unrelated. Using mixin classes is not an option: I will have many such functions and would end up adding a base class for each function. My current "workaround" would be to wrap this function in a "factory" like this:

def create_do_something(): def do_something_usefull(self,arg): self.do_work()

我总是得到一个新的do_something_useful函数,但是我必须像这样包装所有函数。

That way I always get a new do_something_useful function, but I have to wrap all my functions like this.

您可以相信我,我知道,这不是常规 OO编程。我知道如何正常解决类似问题。但这是一个动态代码生成器,我想使所有内容尽可能轻巧和简单。而且由于python函数是非常普通的对象,所以我不问问如何复制它们就太奇怪了!?

You can trust me, that I'm aware, that this is no "normal" OO programming. I know how to solve something like that "normally". But this is a dynamic code generator and I would like to keep everything as lightweight and simple as possible. And as python functions are quite normal objects, I don't think it's too strange to ask how to copy them!?

推荐答案

在 Python3 中:

import types import functools def copy_func(f): """Based on stackoverflow/a/6528148/190597 (Glenn Maynard)""" g = types.FunctionType(f.__code__, f.__globals__, name=f.__name__, argdefs=f.__defaults__, closure=f.__closure__) g = functools.update_wrapper(g, f) g.__kwdefaults__ = f.__kwdefaults__ return g def f(arg1, arg2, arg3, kwarg1="FOO", *args, kwarg2="BAR", kwarg3="BAZ"): return (arg1, arg2, arg3, args, kwarg1, kwarg2, kwarg3) f.cache = [1,2,3] g = copy_func(f) print(f(1,2,3,4,5)) print(g(1,2,3,4,5)) print(g.cache) assert f is not g

收益率

(1, 2, 3, (5,), 4, 'BAR', 'BAZ') (1, 2, 3, (5,), 4, 'BAR', 'BAZ') [1, 2, 3]

在 Python2 中:

import types import functools def copy_func(f): """Based on stackoverflow/a/6528148/190597 (Glenn Maynard)""" g = types.FunctionType(f.func_code, f.func_globals, name=f.func_name, argdefs=f.func_defaults, closure=f.func_closure) g = functools.update_wrapper(g, f) return g def f(x, y=2): return x,y f.cache = [1,2,3] g = copy_func(f) print(f(1)) print(g(1)) print(g.cache) assert f is not g

收益率

(1, 2) (1, 2) [1, 2, 3]

更多推荐

如何创建python函数的副本

本文发布于:2023-10-25 03:08:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1525744.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:副本   函数   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!