instance introspection and __slots__

Jeremy Hylton jeremy at alum.mit.edu
Wed Oct 16 13:07:44 EDT 2002


mertz at gnosis.cx (David Mertz, Ph.D.) wrote in message news:<mailman.1034743521.2929.python-list at python.org>...
> I'm probably just being daft here... but would you show me an object
> that has both __dict__ and __slots__? 

Python2.3:

>>> class Foo(object):
...     __slots__ = "slot"
... 
>>> class Bar(Foo):
...     pass
... 
>>> b = Bar()
>>> b.__slots__
'slot'
>>> b.slot = 2
>>> b.frob = 1
>>> b.__dict__
{'frob': 1}
>>> import pickle
>>> pickle.dumps(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/local/lib/python2.3/pickle.py", line 1063, in dumps
    Pickler(file, bin).dump(object)
  File "/usr/local/lib/python2.3/pickle.py", line 167, in dump
    self.save(object)
  File "/usr/local/lib/python2.3/pickle.py", line 241, in save
    tup = reduce()
  File "/usr/local/lib/python2.3/copy_reg.py", line 68, in _reduce
    dict = getstate()
TypeError: a class that defines __slots__ without defining
__getstate__ cannot be pickled



More information about the Python-list mailing list