Pickling a class instead of an instance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sebastien Boisgerault

    #1

    Pickling a class instead of an instance

    Hi,

    It seems to me that it's not possible with the pickle module
    to serialize a class rather than an instance, as in
    [color=blue][color=green]
    >> from pickle import *
    >>
    >> class C(object):
    >> "... doc ..."
    >> a = 1
    >>
    >> pickstr = dumps(C)[/color][/color]

    I mean, it does *something*, there is no error indeed, but
    from the string pickstr, I am unable to rebuild the class
    C in a brand new context (got a "you're really stupid, all
    you deserve is an AttributeError because you know there is
    no attribute 'C' in the 'module' object" error).

    Am I wrong ? Why would the "(new-style) classes are regular
    objects too" mantra not apply in this case ? Could we imagine
    a patch to the pickle module to handle this kind of situation ?

    SB
  • harold fellermann

    #2
    Re: Pickling a class instead of an instance

    have a look at the thread "copying classes?" some days ago.
    what goes for copying goes for pickling also, because the
    modules use the same interface.

    - harold -

    On 13.01.2005, at 13:32, Sebastien Boisgerault wrote:
    [color=blue]
    > Hi,
    >
    > It seems to me that it's not possible with the pickle module
    > to serialize a class rather than an instance, as in
    >[color=green][color=darkred]
    >>> from pickle import *
    >>>
    >>> class C(object):
    >>> "... doc ..."
    >>> a = 1
    >>>
    >>> pickstr = dumps(C)[/color][/color]
    >
    > I mean, it does *something*, there is no error indeed, but
    > from the string pickstr, I am unable to rebuild the class
    > C in a brand new context (got a "you're really stupid, all
    > you deserve is an AttributeError because you know there is
    > no attribute 'C' in the 'module' object" error).
    >
    > Am I wrong ? Why would the "(new-style) classes are regular
    > objects too" mantra not apply in this case ? Could we imagine
    > a patch to the pickle module to handle this kind of situation ?
    >
    > SB
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    >[/color]
    --
    If you make people think they're thinking, they'll love you;
    but if you really make them think they'll hate you.

    Comment

    Working...