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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:42:43+00:00 2026-05-11T07:42:43+00:00

Putting aside whether the use of isinstance is harmful , I have run into

  • 0

Putting aside whether the use of isinstance is harmful, I have run into the following conundrum when trying to evaluate isinstance after serializing/deserializing an object via Pickle:

from __future__ import with_statement import pickle  # Simple class definition class myclass(object):     def __init__(self, data):         self.data = data  # Create an instance of the class x = myclass(100)  # Pickle the instance to a file with open('c:\\pickletest.dat', 'wb') as f:     pickle.dump(x, f)  # Replace class with exact same definition class myclass(object):     def __init__(self, data):         self.data = data  # Read an object from the pickled file with open('c:\\pickletest.dat', 'rb') as f:     x2 = pickle.load(f)  # The class names appear to match print x.__class__ print x2.__class__  # Uh oh, this fails...(why?) assert isinstance(x2, x.__class__) 

Can anyone shed some light on why isinstance would fail in this situation? In other words, why does Python think these objects are of two different classes? When I remove the second class definition, isinstance works fine.

  • 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. 2026-05-11T07:42:44+00:00Added an answer on May 11, 2026 at 7:42 am

    This is how the unpickler works (site-packages/pickle.py):

    def find_class(self, module, name):     # Subclasses may override this     __import__(module)     mod = sys.modules[module]     klass = getattr(mod, name)     return klass 

    To find and instantiate a class.

    So of course if you replace a class with an identically named class, the klass = getattr(mod, name) will return the new class, and the instance will be of the new class, and so isinstance will fail.

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

Sidebar

Related Questions

If we put aside the rights and wrongs of putting demo data into a
MySQL is putting off the following error: You have an error in your SQL
I've been putting a lot of thought into procedural generation of content for a
I am putting together some ideas for our automated testing platform and have been
Putting aside, for now, the arguments about the relative virtues and disvirtues of the
I have come across the following type of code many a times, and I
I'm trying to resolve the following problem: [.Net 2.0, CLR 2.5.something, 64 bit machine,
I like putting shortcuts of the form g - google.lnk in my start menu
We are putting up a company blog at companyname.com/blog but for now the blog
I am putting together a proposal for a large multinational company for our licenced

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.