类型错误:无法深度复制此模式对象
试图理解我的“变量”类中的这个错误。
我希望在我的“Variable”类中存储 sre.SRE_Pattern 。我刚刚开始复制 Variable 类,并注意到它导致我的所有 Variable 类实例发生更改。我现在明白我需要深度复制此类,但现在遇到“TypeError:无法深度复制此模式对象”。当然,我可以将模式存储为文本字符串,但我的代码的其余部分已经需要编译模式!使用模式对象复制变量类的最佳方法是什么?
import re
from copy import deepcopy
class VariableWithRE(object):
"general variable class"
def __init__(self,name,regexTarget,type):
self.name = name
self.regexTarget = re.compile(regexTarget, re.U|re.M)
self.type = type
class VariableWithoutRE(object):
"general variable class"
def __init__(self,name,regexTarget,type):
self.name = name
self.regexTarget = regexTarget
self.type = type
if __name__ == "__main__":
myVariable = VariableWithoutRE("myName","myRegexSearch","myType")
myVariableCopy = deepcopy(myVariable)
myVariable = VariableWithRE("myName","myRegexSearch","myType")
myVariableCopy = deepcopy(myVariable)
Trying to understand this error in my "Variable" class.
I was hoping to store a sre.SRE_Pattern in my "Variable" class. I just started copying the Variable class and noticed that it was causing all my Variable class instances to change. I now understand that I need to deepcopy this class, but now I run into "TypeError: cannot deepcopy this pattern object". Sure, I can store the pattern as a text string but the rest of my code already expects a compiled pattern! What would be the best way to copy my Variable class with a pattern object?
import re
from copy import deepcopy
class VariableWithRE(object):
"general variable class"
def __init__(self,name,regexTarget,type):
self.name = name
self.regexTarget = re.compile(regexTarget, re.U|re.M)
self.type = type
class VariableWithoutRE(object):
"general variable class"
def __init__(self,name,regexTarget,type):
self.name = name
self.regexTarget = regexTarget
self.type = type
if __name__ == "__main__":
myVariable = VariableWithoutRE("myName","myRegexSearch","myType")
myVariableCopy = deepcopy(myVariable)
myVariable = VariableWithRE("myName","myRegexSearch","myType")
myVariableCopy = deepcopy(myVariable)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
deepcopy
对您的类一无所知,也不知道如何复制它们。您可以通过实现
__deepcopy__()
方法来告诉deepcopy
如何复制对象:deepcopy
doesn't know anything about your classes and doesn't know how to copy them.You can tell
deepcopy
how to copy your objects by implementing a__deepcopy__()
method:这可以通过在 3.7 之前的 python 中修补
copy
模块来解决:This can be worked around by patching
copy
module in pre-3.7 python:这似乎在 Python 3.7+ 版本中得到了修复:
按照:https://docs .python.org/3/whatsnew/3.7.html#re
测试:
This seems to be fixed in Python version 3.7+:
as per: https://docs.python.org/3/whatsnew/3.7.html#re
Testing:
问题似乎是编译的正则表达式。
deepcopy
无法处理它们。一个最小的示例给了我同样的错误:
这会引发错误:
TypeError:无法深度复制此模式对象
。我在python3.5。The problem seems to be the compiled regex.
deepcopy
cannot handle them.A minimal example gives me the same error:
This throws the error:
TypeError: cannot deepcopy this pattern object
. I'm in python3.5.如果此类(及其子类_)的实例不需要需要深度复制,但只会导致问题,因为它们是做<的对象图的一部分/strong> 需要深复制,那么你可以这样做:
现在,你需要小心了。我所做的假设。如果您开始更改此类的任何实例,则可能会出现副作用,因为深层复制实际上并未发生。仅当您在其他地方需要深度复制并且您非常确定您不关心此类和深度复制时,才值得这样做。
IF your instances of this class (and its subclasses_) don't need deep-copy but are only causing problems because they are part of object graphs that do need deep-copy, then you could do this:
Now, you need to be careful re. the assumption I am making. If you start altering any instances of this class, you are at risk of having side effects appear because the deepcopy did not in fact take place. It's only worthwhile if you need deepcopy elsewhere and you are pretty sure you don't care about this class and deepcopy.