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

Brandon Mintern bmintern at gmail.com
Fri May 23 01:22:24 CEST 2008


On Thu, May 22, 2008 at 7:15 PM, Leif Walsh <adlaiff6 at gmail.com> wrote:
> On Thu, 22 May 2008, Brandon Mintern wrote:
>> The implementation:
>>
>> class struct (object):
>>
>>     <things here>
>>
>>     def __setattr__ (self, name, value):
>>         """
>>         I think it makes the most sense for a struct to have immutable
>>         fields. As soon as you start to add more fields, you should be
>>         using something other than a struct.
>>         """
>>         if name in self.__dict__:
>>             self.__dict__[name] = value
>>         else:
>>             raise(AttributeError("'struct' object has no attribute '%s'" \
>>                                  % (name,)))
>
> I think it makes the most sense, if this construct is adopted, to use
> __slots__ to control mutability.  Someone more well-versed in the
> python object model should determine if this is actually a good idea.
>
> --
> Cheers,
> Leif

Whoops... I should have said "An implementation." I intended the
implementation to be a specification for behavior and not the
one-and-only implementation. Personally, I'm not especially familiar
with using __slots__ in anything but standard usage, so I found it
easier to show the code using __dict__. Certainly if __slots__ can be
used, it avoids the need to write __setattr__ and to explicitly raise
the AttributeError exception (which I already see is wrong because it
doesn't support inheritance).



More information about the Python-ideas mailing list