Factory for Struct-like classes
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Wed Aug 20 04:53:53 EDT 2008
En Wed, 20 Aug 2008 04:00:08 -0300, Dan Lenski <dlenski at gmail.com>
escribi�:
> On Mon, 18 Aug 2008 08:28:53 -0700, Dan Lenski wrote:
>> So is there a bug in the Python docs? Does __slots__ in fact work with
>> subclasses of tuple?
>>
> Anybody think that this may actually be a mistake in the Python docs?
It appears that __slots__ must be empty in those cases. Trying to use
anything non-empty raises a TypeError:
>>> class xtuple(tuple):
... __slots__ = ('abc',)
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: nonempty __slots__ not supported for subtype of 'tuple'
So __slots__ partially works - it avoids creating an instance __dict__,
but you can't add additional instance attributes.
But there is a point I don't understand yet - by example, array.array
should fail too, same as tuple, AFAICT it is declared using
PyObject_VAR_HEAD. But __slots__ works fine with it:
>>> from array import array
>>> class xarray(array):
... __slots__ = ('foo','bar')
...
>>> a = xarray('b', [1,2,3])
>>> a
array('b', [1, 2, 3])
>>> a.foo
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: foo
(Note the short error message above: I guess it comes from the "foo" slot
containing NULL)
>>> a.foo = 123
>>> a.foo
123
>>> a.xxx
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'xarray' object has no attribute 'xxx'
>>> a.xxx = 123
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'xarray' object has no attribute 'xxx'
So I don't get *what* exactly makes a type reject __slots__ - it's not the
variable part alone as the docs imply (and I can't find the error message
anywhere to check the actual code)
> Who
> would I contact about getting them corrected?
I think you should report it at http://bugs.python.org/ -- best including
a documentation patch, if possible.
--
Gabriel Genellina
More information about the Python-list
mailing list