__slots__ and class attributes

Ewald R. de Wit erdewit at d-e-l-e-t-e.zonnet.nl
Thu Nov 3 08:25:18 EST 2005


I'm running into a something unexpected  for a new-style class
that has both a class attribute and __slots__ defined. If the
name of the class attribute also exists in __slots__, Python
throws an AttributeError. Is this by design (if so, why)?

class A( object ):
	__slots__ = ( 'value', )
	value = 1

	def __init__( self, value = None ):
		self.value = value or A.value

a = A()
print a.value


Traceback (most recent call last):
  File "t1.py", line 8, in ?
    a = A()
  File "t1.py", line 6, in __init__
    self.value = value or A.value
AttributeError: 'A' object attribute 'value' is read-only





More information about the Python-list mailing list