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 6063857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:12:55+00:00 2026-05-23T09:12:55+00:00

In python 2.6, I defined a class Abc, made a dict d where keys

  • 0

In python 2.6, I defined a class Abc, made a dict d where keys are strings and values are abc objects. Then I dumped this dict to a file like this-
pickle.dump(d, open(‘filename.pkl’, ‘wb’))

I can successfully load it python 2.6 with

d1 = pickle.load(open('filename.pkl', 'rb'))

(d1 is identical to d)

But when I try to load it in python 3.1, only the first key (string) is loaded

d1 = pickle.load(open('filename.pkl', 'rb'))

print(d1)
keyname1

The pickled file starts like

(dp0
S'keyname1'
p1
(iabc
ABC
p2 

So it seems like in 3.1, it’s only loading the first string in the file. I don’t know much about encoding etc. Any ideas on what I can do to load my dict d in 3.1.1?

Edit- my class (abc.py) is in the same working directory both times, not sure if this matters?

Edit after answer- I realized I should have clarified better so it can be reproduced easily. My code was buggy both because of the name clash and because I wasn’t defining my class correctly.

Anyways, I just tried the following and learnt that I should start classes with class ABC(object): rather than just class ABC. And that I should delete .pyc files before importing a module the second time.

What I did —

Try 1

ABC.py

class ABC():
   pass

in python-2.6

import pickle
import ABC
d = {'keyname1': ABC.ABC()}
pickle.dump(d, open('filename.pkl', 'wb'))
quit()

in python-3.1

import pickle
d1 = pickle.load(open('filename.pkl', 'rb'))
print(d1)
 keyname1
quit()

Try 2

But when I changed the class definition in ABC.py so it said class ABC(object): instead of just class ABC:, removed the old filename.pkl, ABC.pyc and repeated,

in python-2.6

# same as before

in python-3.1

import pickle
d1 = pickle.load(open('filename.pkl', 'rb'))
print(d1)
{'keyname1': <ABC.ABC object at 0x638890>}
  • 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-23T09:12:55+00:00Added an answer on May 23, 2026 at 9:12 am

    The following works fine with Python 2.6.6 and Python 3.1.3:

    Python 2.6 code:

    import pickle
    
    class ABC(object):
        pass
    d = {'keyname1': ABC()}
    
    # Dump!
    pickle.dump(d, open('filename.pkl', 'wb'))
    

    Python 3.1 code:

    import pickle
    
    class ABC(object):
        pass
    
    # Load:
    d1 = pickle.load(open('filename.pkl', 'rb'))
    
    print(d1)
    

    The result is:

    {'keyname1': <__main__.ABC object at 0x8c99eec>}
    

    So clearly loading the ABC object worked fine.
    I think the problem is that you have a name clash between your module abc and the module called abc in the standard library, and that this name clash behaves differently in Python 2 and Python 3.

    Solution: Rename your module.

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

Sidebar

Related Questions

I want to create an instance of a Python class defined in the __main__
The descriptor protocol in Python 2.6 is only defined for class definitions, and thus
I have a python module that defines a number of classes: class A(object): def
With a class in Python, how do I define a function to print every
The python 2.6 docs state that x % y is defined as the remainder
Python has this wonderful way of handling string substitutions using dictionaries: >>> 'The %(site)s
Python gives us the ability to create 'private' methods and variables within a class
Really confused about what's going on here. I have a class defined as follows:
When creating a simple object hierarchy in Python, I'd like to be able to
I have a compiled Python library and API docs that I would like to

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.