Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: changing dictionary returned by groupdict()

Is it safe to modify a mutable object returned by a method of a standard library object?

Here's one specific example; but I'm looking for a general answer if possible.

#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

Is this code safe? I'm concerned that by changing groupdict() I'm corrupting the object m (which I still need for later).

I tested this out, and a subsequent call to m.groupdict() still returned the original dictionary; but for all I know this may be implementation-dependent.

like image 996
max Avatar asked Nov 15 '22 08:11

max


1 Answers

Generally it's only guaranteed to be safe if the documentation says so. (In this particular case it seems very unlikely that another implementation would behave differently though.)

like image 134
JanC Avatar answered Nov 17 '22 04:11

JanC