Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:无法复制此图案对象_Python_Regex_Deep Copy - Fatal编程技术网

Python TypeError:无法复制此图案对象

Python TypeError:无法复制此图案对象,python,regex,deep-copy,Python,Regex,Deep Copy,试图在我的“变量”类中理解此错误 我希望在我的“Variable”类中存储一个sre.sre_模式。我刚开始复制变量类,注意到它导致我所有的变量类实例都发生了变化。我现在明白了我需要deepcopy这个类,但现在我遇到了“TypeError:cannotdeepcopy this pattern object”。当然,我可以将模式存储为文本字符串,但我的代码的其余部分已经需要编译模式了!用模式对象复制变量类的最佳方法是什么 import re from copy import deepcopy

试图在我的“变量”类中理解此错误

我希望在我的“Variable”类中存储一个sre.sre_模式。我刚开始复制变量类,注意到它导致我所有的变量类实例都发生了变化。我现在明白了我需要deepcopy这个类,但现在我遇到了“TypeError:cannotdeepcopy this 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)

deepcopy
对您的类一无所知,也不知道如何复制它们

通过实现
\uuu deepcopy\uuu()
方法,您可以告诉
deepcopy
如何复制对象:

class VariableWithoutRE(object):
   # ...
   def __deepcopy__(self):
      return VariableWithoutRE(self.name, self.regexTarget, self.type)

问题似乎在于已编译的正则表达式<代码>深度复制无法处理它们

一个最小的例子给了我同样的错误:

import re,copy
class C():
    def __init__(self):
        self.regex=re.compile('\d+')

myobj = C()    
copy.deepcopy(myobj)

这会引发错误:
TypeError:无法深度复制此模式对象
。我使用的是python3.5。

这似乎在python3.7+版本中得到了修复:

现在可以使用copy.copy()和copy.deepcopy()复制编译的正则表达式和匹配对象。(由Serhiy Storchaka在bpo-10076中提供。)

根据:

测试:

import re,copy

class C():
    def __init__(self):
       self.regex=re.compile('\d+')

myobj = C()    
foo = copy.deepcopy(myobj)
foo.regex == myobj.regex
# True

这可以通过在3.7之前的python中修补
copy
模块来解决:

import copy
import re 

copy._deepcopy_dispatch[type(re.compile(''))] = lambda r, _: r

o = re.compile('foo')
assert copy.deepcopy(o) == o

如果该类(及其子类)的实例不需要深度复制,但只会导致问题,因为它们是对象图的一部分,需要深度复制,则可以执行以下操作:

#might as well limit this hack to versions that need it...
if sys.version_info <= (3, 7):

    def __deepcopy__(self, *args, **kwargs):
        """ cheat here because regex can't be deepcopied"""
        return self
#还不如将此攻击限制在需要它的版本上。。。

如果sys.version_info给定编译器regexp是不可变的,则无需对其进行深度复制。但是,我不记得如何告诉
deepcopy()
如何处理特定类型(但请注意,如果需要,可以向内置类型添加属性)。代码引发异常的哪一行?我已经复制了它并编译了->它在我这边没有任何错误。我发现的唯一一件事是,您尝试使用重写python方法“type”的变量,但这不是很好的样式。最后一行在python 2.6中为我抛出了错误。问题是要复制
VariableWithRE
,所以您的示例没有真正的帮助,但方向正确。我在两个类中都尝试了此方法,首先我得到了“TypeError:uuu deepcopy_uu()正好接受1个参数(给定2个)”,然后我查看了这个问题,并尝试了“def u deepcopy(self,memo):”。语法对我来说似乎很奇怪,因为有人还在学习Python,但好吧,备忘录,很好。这在VariableWithout类中有效,但在VariableWithRE类中仍然存在问题!新错误:“ValueError:无法使用编译模式“@user789215”处理标志参数问题是,您正在使用已编译的正则表达式(self.regexTarget)调用
VariableWithoutRE
构造函数(init),而构造函数需要字符串。这会创建新实例吗?从我的测试来看,它看起来像它,但代码表明它是同一个实例?@MarkusRessel:它不是——如
断言复制所示。deepcopy(o)是o
。你是怎么测试的?你是对的,我的测试是错的。
re.compile()
方法具有内置缓存,因此即使在lambda中使用类似于
re.compile(r.pattern)
的东西,如果模式(和标志)相同,也不会创建新实例。我想知道在python 3.7中是否也是这样做的。它实际上是相同的(即:copy被实现为identity),但在re模块的本机代码中实现:我有同样的问题