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

  • SEARCH
  • Home
  • 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 9099493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:37:59+00:00 2026-06-17T00:37:59+00:00

Running: import pickle class Foo(str): def __new__(cls, s, bar): self = super(Foo, cls).__new__(cls, s)

  • 0

Running:

import pickle

class Foo(str):
    def __new__(cls, s, bar):
        self = super(Foo, cls).__new__(cls, s)
        self.bar = bar
        return self

with open('foo.pkl', 'wb') as w:
    x = pickle.dump(Foo('', 1), w, pickle.HIGHEST_PROTOCOL)

with open('foo.pkl', 'rb') as w:
    x = pickle.load(w)

Results in this exception:

Traceback (most recent call last):
  File "pkl.py", line 13, in <module>
    x = pickle.load(w)
  File "/usr/lib/python2.7/pickle.py", line 1378, in load
    return Unpickler(file).load()
  File "/usr/lib/python2.7/pickle.py", line 858, in load
    dispatch[key](self)
  File "/usr/lib/python2.7/pickle.py", line 1083, in load_newobj
    obj = cls.__new__(cls, *args)
TypeError: __new__() takes exactly 3 arguments (2 given)

It works, if I remove pickle.HIGHEST_PROTOCOL, but I’d rather use the new protocol, if possible.

I’m just not getting the pickle protocol description. Can anyone help me out? There must be some way with magic methods…

  • 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-06-17T00:38:00+00:00Added an answer on June 17, 2026 at 12:38 am

    This works without the HIGHEST_PROTOCOL because the older protocols don’t use the __new__ constructor for unpickled objects.

    But the basic is that you are using the serializing methods of str while you are adding a new argument to __new__. The pickle module nor the builtin str type has any knowledge of this and it will result in the error shown.

    To fix it you should define the __getnewargs__ method and return (self.__class__, self.bar) which should be sufficient for it to work again.

    Documentation on the special methods used by the pickle module can be found here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running this simple code: import threading, time class reqthread(threading.Thread): def run(self): for i
Here is the situation: I've got a command I'm running: import subprocess foo =
When running the following class the ExecutionService will often deadlock. import java.util.ArrayList; import java.util.Collection;
I'm running play application. I have import org.junit.Before; public class Frontpage extends Controller {
How can I import a python class but avoid running the import statement in
Running on JBoss AS7, I have this: import javax.inject.Singleton; @Singleton public class Connections {
I am running some import code asynchronously from a simple WinForms app using a
My program is now still running to import data from a log file into
After changing the import as a from-import i'm running into this error: from datetime
I'm running SQL Server 2005 Express. And I'm trying to do a bulk insert/import

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.