Problem of Readability of Python

Michele Simionato michele.simionato at gmail.com
Mon Oct 8 06:47:26 CEST 2007


On Oct 7, 7:58 pm, al... at mac.com (Alex Martelli) wrote:
> If you REALLY pine for Pascal's records, you might choose to inherit
> from ctypes.Structure, which has the additional "advantages" of
> specifying a C type for each field and (a real advantage;-) creating an
> appropriate __init__ method.
>
> >>> import ctypes
> >>> class Record(ctypes.Structure):
>
> ...  _fields_ =
> (('x',ctypes.c_float),('y',ctypes.c_float),('z',ctypes.c_float)
> )
> ...>>> r=Record()
> >>> r.x
> 0.0
> >>> r=Record(1,2,3)
> >>> r.x
> 1.0
> >>> r=Record('zip','zop','zap')
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError:  float expected instead of str instance
>
> See?  You get type-checking too -- Pascal looms closer and closer!-)
>
> And if you need an array of 1000 such Records, just use as the type
> Record*1000 -- think of the savings in memory (no indirectness, no
> overallocations as lists may have...).

That's very cool Alex! I have just a question: suppose I want
to measure the memory allocation of a million of records made
with ctypes vs the memory allocation of equivalent
records made with __slots__, how do I measure it? Say on Linux,
Mac and Windows?
If ctypes records are more efficient than __slots__ records,
I will ask for deprecation of __slots__ (which is something
I wanted from the beginning!).


   Michele Simionato




More information about the Python-list mailing list