и вы увидите, что они разные
ив ли использованиеэто вредноЯ столкнулся со следующей загадкой при попытке оценить isinstance после сериализации / десериализации объекта с помощью 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__)
Может ли кто-нибудь пролить свет на то, почему в этой ситуации произойдет сбой? Другими словами, почему Python считает эти объекты двух разных классов? Когда я удаляю определение второго класса,isinstance
работает отлично.