On Fri, May 23, 2008 at 12:51 AM, George Sakkis
On Thu, May 22, 2008 at 9:59 PM, Greg Ewing
wrote: On Thu, May 22, 2008 at 7:15 PM, Leif Walsh
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__'
The recipe here does roughly that (though without the defaulting extras): http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/502237 Note however that use of __slots__ is still controversial. (Because it's really only appropriate if you plan to make many of these objects and you want to keep memory consumption down.) Steve -- I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a tiny blip on the distant coast of sanity. --- Bucky Katt, Get Fuzzy