On Thu, May 22, 2008 at 9:59 PM, Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
On Thu, May 22, 2008 at 7:15 PM, Leif Walsh <adlaiff6@gmail.com> wrote:
I think it makes the most sense, if this construct is adopted, to use __slots__ to control mutability.
That wouldn't work, because the slots that a class has are fixed when the class is defined. Changing __slots__ subsequent to that doesn't affect anything.
Unless you generate the class on the fly with a class factory, like namedtuple. Actually I prefer a metaclass factory, to avoid the repetition of the class name: >>> class Foo(object): ... __metaclass__ = RecordType('x y z is_on', field_defaults={'is_on':False}, default=0.0) >>> a = Foo() >>> a Foo(x=0.0, y=0.0, z=0.0, is_on=False) >>> a.__slots__ ('x', 'y', 'z', 'is_on') >>> a.__dict__ Traceback (most recent call last): AttributeError: 'Foo' object has no attribute '__dict__' George