修改标准库对象的方法返回的可变对象是否安全?
这是一个具体的例子;但如果可能的话,我正在寻找一个普遍的答案。
#m is a MatchObject
#I know there's only one named group in the regex
#I want to retrieve the name and the value
g, v = m.groupdict().popitem()
#do something else with m
这段代码安全吗?我担心通过更改 groupdict() 我会破坏对象 m (我以后仍然需要它)。
我对此进行了测试,随后对 m.groupdict() 的调用仍然返回了原始字典;但据我所知,这可能取决于实现。