Pickle problem

Michael Stenner mstenner at phy.duke.edu
Tue Sep 10 08:57:37 EDT 2002


On Tue, Sep 10, 2002 at 01:44:57PM +0100, Harvey Thomas wrote:
> Erik Price wrote:
> > 
> > On Tuesday, September 10, 2002, at 06:13  AM, Harvey Thomas wrote:
> > 
> > > I'm trying to pickle a complicated data structure which includes a 
> > > dictionary whose entries are created by (simplified):
> > >
> > > a[x] = re.compile(y).match
> > >
> > > The pickle fails, however, with the error:
> > >
> > > PicklingError: Can't pickle <built-in method match of 
> > _sre.SRE_Pattern 
> > > object at
> > >  0x00838700>: it's not found as __main__.match
> > 
> > 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.

Are you interested in storing the "match results" or the "match
method"?  I think Erik was suggesting that you may want

a[x] = re.compile(y).match()

That will pickle the match object.  What you're doing is trying to
pickle the method (function) that gets called to return the result.

Example:

class foo:
    def bar(): return "some data"

f = foo()

pickling "f.bar" is very different from pickling "f.bar()" because
the former is a method, and the latter is a string (specifically,
"some data").

					-Michael
-- 
  Michael Stenner                       Office Phone: 919-660-2513
  Duke University, Dept. of Physics       mstenner at phy.duke.edu
  Box 90305, Durham N.C. 27708-0305




More information about the Python-list mailing list