RE: [Twisted-Python] Nested objects in pickle (was: Large projec t (IMS) architecture)
that's a good question:
class MyClass: def __init__(self): self.blah = 1
class MySecondClass: def __init__(self): self.foo = MyClass()
class MyThirdClass: def MyFunc(): print "test"
bar = MyThirdClass() bar.foo = MySecondClass() dir(bar) ['MyFunc', '__doc__', '__module__', 'foo']
pickle.dump(bar, outfile) |
| \/ (i__main__ MyThirdClass p0 (dp1 S'foo' p2 (i__main__ MySecondClass p3 (dp4 g2 (i__main__ MyClass p5 (dp6 S'blah' p7 I1 sbsbsb.
blarg = pickle.load(infile) dir(blarg) ['MyFunc', '__doc__', '__module__', 'foo']
blarg.foo.foo.blah 1
The has-a nested relationships seem to work. Is it the is-a that is broken in pickle? I never knew that to be a nested relationship. -----Original Message----- From: twisted-python-admin@twistedmatrix.com [mailto:twisted-python-admin@twistedmatrix.com]On Behalf Of Patrick K. O'Brien Sent: Thursday, January 16, 2003 10:39 AM To: twisted-python@twistedmatrix.com; Steve Waterbury Subject: Re: [Twisted-Python] Large project (IMS) architecture On Thursday 16 January 2003 12:01 pm, Steve Waterbury wrote:
Itamar Shtull-Trauring wrote:
PB actually implements its own version of Pickle as well (jelly)...
And jelly does nested objects, which Pickle can't.
I'm not sure what you mean by that. Could you explain in what way Pickle can't handle nested objects? Thanks. -- Patrick K. O'Brien Orbtech http://www.orbtech.com/web/pobrien ----------------------------------------------- "Your source for Python programming expertise." ----------------------------------------------- _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
On Thursday 16 January 2003 01:09 pm, Rick Richardson wrote:
The has-a nested relationships seem to work. Is it the is-a that is broken in pickle? I never knew that to be a nested relationship.
Not quite. The real issue concerns classes and functions defined within other classes, functions, or methods. Pickling only works on objects defined at the "top" level of a module. Here is an example:
import pickle class Foo: ... def __init__(self): ... class Bar: ... def __init__(self): ... self.type = 'Nested class' ... self.bar = Bar() ... foo = Foo() foo.bar.type 'Nested class' s = pickle.dumps(foo) foo2 = pickle.loads(s) Traceback (most recent call last): File "<stdin>", line 1, in ? File "/usr/local/lib/python2.2/pickle.py", line 986, in loads return Unpickler(file).load() File "/usr/local/lib/python2.2/pickle.py", line 597, in load dispatch[key](self) File "/usr/local/lib/python2.2/pickle.py", line 767, in load_inst klass = self.find_class(module, name) File "/usr/local/lib/python2.2/pickle.py", line 824, in find_class klass = getattr(mod, name) AttributeError: 'module' object has no attribute 'Bar'
For more pickling details see my article at http://www-106.ibm.com/developerworks/library/l-pypers.html as well as the section of the Python documentation dealing with Pickling limitations at http://www.python.org/doc/current/lib/node62.html. -- Patrick K. O'Brien Orbtech http://www.orbtech.com/web/pobrien ----------------------------------------------- "Your source for Python programming expertise." -----------------------------------------------
participants (2)
-
Patrick K. O'Brien
-
Rick Richardson