python中的正则表达式匹配组内存分配和清除

2024-04-19 05:25:51 发布

您现在位置:Python中文网/ 问答频道 /正文

正则表达式中的matchobj是否分配了单独的内存? 我看到它被分配了不同的内存地址(id()与字符串测试不同)

test=....
>>> import re
>>> matchobj=re.match(r'(.*) key=({.*}) .*', test)
>>> matchobj.group(2)
'{xxxx#####xxxx}'
>>> type(matchobj)
<type '_sre.SRE_Match'>
>>> id(matchobj)
4344654000
>>> id(test)
4343948752
>>> 
>>> id(matchobj.group(2))
4344883632
>>> type(matchobj.group(2))
<type 'str'>

但我现在无法清除数据匹配对象组(2) 使用ctypes.memset文件. 你知道吗

>>> import sys
>>> import ctypes
>>> size=sys.getsizeof(matchobj.group(2))
>>> buf=len(matchobj.group(2))+1
>>> offset=size - buf
>>> ctypes.memset(id(matchobj.group(2)) + offset, 0, buf)
4344884756
>>> offset
36
>>> matchobj.group(2)
'{xxxx#####xxxx}'
>>> 

为什么我们不能清除分配给我们的记忆匹配对象组(2) 如果只是一根线?你知道吗


Tags: 对象testimportreidsizetypesys