Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4049094
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:54:03+00:00 2026-05-20T13:54:03+00:00

During instantiation of my class, I initialize some fields that are not picklable. Thus,

  • 0

During instantiation of my class, I initialize some fields that are not picklable. Thus, in order to be able to (un)pickle my classes correctly, I would like my init method to be called on unpickling. This is, it seems, the way it worked with old-style classes.

With new style classes, I need to use __new__ and __getnewargs__. Here is what I do:

import cPickle


class Blubb(object):

  def __init__(self, value):
    self.value = value


class Bla(Blubb):

  def __new__(cls, value):
    instance = super(Bla, cls).__new__(cls)
    instance.__init__(value)
    return instance

  def __getnewargs__(self):
    return self.value,

  def __getstate__(self):
    return {}

  def __setstate__(self, dct):
    pass

x = Bla(2)
print x.value
pickled = cPickle.dumps(x, 2)
x_ = cPickle.loads(pickled)
assert x_.value == 2

This would be fine, if not for the fact that obj = C.__new__(C, *args). There is now **kwargs. So I am restricted to non keyword arguments in my __new__ and __init__ methods.

Does anyone know of a way to solve this? This is really unconvenient.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T13:54:04+00:00Added an answer on May 20, 2026 at 1:54 pm

    The pickle protocol 2 wants to call cls.__new__(cls, *args) by default, but there is a way around this. If you use __reduce__ you can return a function which will map your arguments to __new__. I was able to modify your example to get **kwargs to work:

    import cPickle
    
    class Blubb(object):
    
        def __init__(self, value, foo=None, bar=None):
            self.value = value
            self.foo = foo
            self.bar = bar
    
    def _new_Bla(cls, value, kw):
        "A function to map kwargs into cls.__new__"
        return cls.__new__(cls, value, **kw)
    
    class Bla(Blubb):
    
        def __new__(cls, value, **kw):
            instance = super(Bla, cls).__new__(cls)
            instance.__init__(value, **kw)
            return instance
    
        def __reduce__(self):
            kwargs = {'foo': self.foo, 'bar': self.bar}
            return _new_Bla, (self.__class__, self.value, kwargs), None
    
    x = Bla(2, bar=[1, 2, 3])
    pickled = cPickle.dumps(x, 2)
    y = cPickle.loads(pickled)
    assert y.value == 2
    assert y.bar == [1, 2, 3]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am aware that you can initialize an array during instantiation as follows: String[]
I have a class that might throw any run-time exceptions during initialization. I want
Not during instantiation, but once instantiation of singleton object is done, what will happen
I have difficulties understanding a problem I encountered during instantiation of a QT class
During a typical day programming, I implement functions in a way that I would
During my apprenticeship, I have used NHibernate for some smaller projects which I mostly
During my work with databases I noticed that I write query strings and in
I have defined a class with multiple constructors so that the underlying interfaces are
I have class A that is supposed to inherit class B whose name is
I have an AppController class that looks after view/control in my app in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.