[Python-ideas] Proposal to add new built-in struct (was: Add kwargs to built-in function object)

George Sakkis george.sakkis at gmail.com
Fri May 23 08:51:30 CEST 2008


On Thu, May 22, 2008 at 9:59 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:

> On Thu, May 22, 2008 at 7:15 PM, Leif Walsh <adlaiff6 at 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



More information about the Python-ideas mailing list