[Tutor] object representation

Dave Angel davea at ieee.org
Fri Mar 5 09:01:38 CET 2010


spir wrote:
> On Thu, 04 Mar 2010 09:22:52 -0500
> Dave Angel <davea at ieee.org> wrote:
>
>   
>> Still, slots are important, because I suspect 
>> that's how built-ins are structured, to make the objects so small.
>>     
>
> Sure, one cannot alter their structure. Not even of a direct instance of <object>:
>   
>>>> o = object()
>>>> o.n=1
>>>>         
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: 'object' object has no attribute 'n'
>
>   
>> Now, some objects, probably most of the built-ins, are not extensible.  
>> You can't add new methods, or alter the behavior much.
>>     
>
> This applies to any attr, not only methods, also plain "information":
>   
>>>> s = "abc"
>>>> s.n=1
>>>>         
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: 'str' object has no attribute 'n'
>
>
>   
>> Other objects, 
>> such as instances of a class you write, are totally and very flexible.
>>     
>
> conceptually this is equivalent to have no __slots__ slot. Or mayby they could be implemented using structs (which values would be pointers), instead of dicts. A struct is like a fixed record, as opposed to a dict. What do you think? On the implementation side, this would be much simpler, lighter, and more efficient.
> Oh, this gives me an idea... (to implement so-called "value objects").
>
> Denis
>   
having not played much with slots, my model is quite weak there.  But I 
figure the dictionary is in the implementation structure, along with a 
flag saying that it's readonly.  Each item of such a dictionary would be 
an index into the fixed table in the object.  Like a struct, as you say, 
except that in C, there's no need to know the names of the fields at run 
time.

DaveA



More information about the Tutor mailing list