[Tutor] subtyping builtin type

Mark Lawrence breamoreboy at yahoo.co.uk
Tue Dec 31 17:21:46 CET 2013


On 31/12/2013 15:54, Zachary Ware wrote:
> On Tue, Dec 31, 2013 at 9:22 AM, spir <denis.spir at gmail.com> wrote:
>> Thank you, Oscar & Zachary. I guess thus the way it is done is correct (for
>> my case), is it? Seems your last remark shows the source of my confusion:
>> probably, in past times, I subtyped builtin types and overrided their
>> __new__, thus had to call the original one.
>> Would I have to do this if Source had other __init__ params? Or would it
>> work anyway provided the string remains 1st param? Or what else?
>
> The interactive interpreter is great for this kind of thing, you know
> ;).

Good point, i've been letting the newbies off with this one recently.

>
>>>> class Source(str):
> ...     __slots__ = ['i', 'n', 'a', 'k']

The glossary entry for __slots__ states "A declaration inside a class 
that saves memory by pre-declaring space for instance attributes and 
eliminating instance dictionaries. Though popular, the technique is 
somewhat tricky to get right and is best reserved for rare cases where 
there are large numbers of instances in a memory-critical application." 
  I'll admit that I really don't understand what's tricky about it, can 
someone explain please.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list