Pickle problem

Michael Chermside mcherm at destiny.com
Tue Sep 10 12:26:51 EDT 2002

>>Are you trying to store the MatchObject returned by a call to 
>>a regular 
>>expression object's match() method?  If so, it looks like you're 
>>forgetting the parens at the end of "re.compile(y).match".

> No, I haven't forgotten the parentheses. I'd like to pickle the match method 
 > of the compiled regular expression.

Three different objects to consider.

  (1) "the compiled regular expression object":  re.compile(y)
  (2) "the match method":                        re.compile(y).match
  (3) "the MatchObject, ie the results":         re.compile(y).match()

Objects (1) and (3) can be pickled, I don't think (2) can. Here's a 
quick summary of what they're there for.

   Using (3) will allow you to examine the results of the match.
   Using (2) will allow you to re-run the match on a different string.
   Using (1) will allow you to perform a match, search, findall, or
      other functions on any string.

Since you can't pickle (2), I suggest storing (3) unless you need to 
re-run it on a different string, then store (1).

-- Michael Chermside

PS: If what you are trying to do is save in the pickle the exact way 
that the "match" method works because you are afraid that the 
maintainers of Python may come out with a new version of Python in which 
match works differently and is no longer backward compatible, and you 
want your code to still work the same way in this hypothetical new 
version of Python, then you really *DO* need to store (2). 
Unfortunately, it's still not possible (AFAIK). Fortunately, the 
maintainers Python wouldn't do that to you.

More information about the Python-list mailing list