如何将所有python实例复制到一个新类? [英] How to copy all python instances to a new class?

查看:1075
本文介绍了如何将所有python实例复制到一个新类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想深入复制一个类的所有实例,但不复制给定类的定义和规则。



给定我有类OriginalClass,它的实例是我存储的变量。当程序继续时,我想能够使用OriginalClass的以前的快照,从中我将只使用值(我不会存储新的,因此没有规则不需要继承)。



上面的简单示例工作,非常类似于我想要做的:

  import copy 

class VM:
类别中的#nothing
pass

VM1 = VM()
VM1.test = dict()
VM1.test ['one'] =hello

def printmytext(VMPast = copy.deepcopy(VM1)):

.es(VMPast.test ['one'])

VM1.test =Bye

printmytext()#Returnshello

但是想法是替换:

  VMPast = copy.deepcopy(VM1)

对于工作代码我不知道如何



原因是,在我的程序中使用deepcopy版本时(这样:)

  VMPast = copy.deepcopy(VM1)
VM1.MyMethod(VMPast.MyButton [-1])$ ​​b $ b

它会引发以下错误:

  AttributeError:AttributeSetter实例没有__call__方法

因为:

  VMPast.MyButton [-1] 

已发送为:

 '__ deepcopy__'
pre>

而不是实际的替代值我会打算...(按钮),所以如果VMPast已经是虚拟机内的实例的工作副本,这将工作inestad返回 deepcopy
另一件事是,实例是动态的,我不知道提前的名称或值,原始类将在那一刻。

解决方案

深层副本从不复制类定义,只有实例属性。



它创建一个新的实例。 Python实例只保存类定义的引用。只需在

上调用 MyMethod 即可:

  VMPast.MyMethod()



如果类有这样的方法,



这是您的特定,阻止成功的深层复制:

  class VariableManagerClass:
def __getattr __(self,attr):
return AttributeSetter(self,attr)

copy.deepcopy()函数查找 .__ deepcopy __()实例,而您的 __ getattr __ 钩子会返回 AttributeSetter 类。



您可以先测试 attr 以防止发生这种情况:

  class VariableManagerClass:
def __getattr __(self,attr):
如果attr.startswith('_'):
raise AttributeError(attr)
return AttributeSetter )

这表示您的类不处理私有属性或特殊方法名。


I would like to make a deep-copy all the instances of a class, but without copying the definitions and rules of given class.

The intended use is, given I have the class "OriginalClass", its instances are the variables I am storing. While the program goes on, I want to be able to use previous "snapshots" of OriginalClass, from which I will only be using the values (I wont store new ones there, so no rules are needed to be inherited).

The above simple example works, and is very similar to what I want to do:

import copy

class VM:
    #nothing in the class
    pass

VM1=VM()
VM1.test=dict()
VM1.test['one']="hello"

def printmytext(VMPast=copy.deepcopy(VM1)):

    g.es(VMPast.test['one'])

VM1.test="Bye"

printmytext() #Returns "hello"

But the idea would be to substitute:

VMPast=copy.deepcopy(VM1)

For the working code I dont know how to write, which will copy the instances and dictionaries but not the definitions.

The reason is, when using the deepcopy version in my program (this way:)

VMPast=copy.deepcopy(VM1)
VM1.MyMethod(VMPast.MyButton[-1])

it throws the following error:

AttributeError: AttributeSetter instance has no __call__ method

Because the value of:

VMPast.MyButton[-1]

Is been sent as:

'__deepcopy__'

Instead of the actual substituted value I would intend... (the button), so if VMPast was already a working copy of the instances inside VM, this would work inestad of returning deepcopy! Another thing is, the instances are dynamic, I dont know in advance the names or values that the original class will have at that moment. Thank you very much!

解决方案

A deep copy never copies the class definition, only the instance attributes.

It instead creates a new instance. Python instances only hold a reference to the class definition. Just call MyMethod on that instance:

VMPast.MyMethod()

provided the class has such a method.

It is your specific class that prevents a successful deep-copy:

class VariableManagerClass:
    def __getattr__(self, attr):
        return AttributeSetter(self, attr)

The copy.deepcopy() function looks for a .__deepcopy__() method on the instance, and your __getattr__ hook instead returns a AttributeSetter class.

You can prevent this by testing your attr a little first:

class VariableManagerClass:
    def __getattr__(self, attr):
        if attr.startswith('_'):
            raise AttributeError(attr)
        return AttributeSetter(self, attr)

This signals that your class does not handle private attributes or special method names.

这篇关于如何将所有python实例复制到一个新类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆