How to convert an object ref to string and the reverse

Skip Montanaro skip at pobox.com
Thu Jun 21 18:44:41 CEST 2001


    Ivan> how is it possible to convert an instance
    Ivan> to string and how to do the reverse - convert the
    Ivan> resulting string in a valid object reference.

Take a look at the pickle and cPickle modules:

    >>> import pickle
    >>> class foo:
    ...   def __init__(self):
    ...     self.a = 1
    ... 
    >>> i = foo()
    >>> i
    <__main__.foo instance at 0x82d1444>
    >>> pickle.dumps(i)
    "(i__main__\nfoo\np0\n(dp1\nS'a'\np2\nI1\nsb."
    >>> j = pickle.loads(pickle.dumps(i))
    >>> j
    <__main__.foo instance at 0x823fb24>

-- 
Skip Montanaro (skip at pobox.com)
(847)971-7098




More information about the Python-list mailing list