[Python-Dev] __slots__ and default values

Aahz aahz@pythoncraft.com
Tue, 13 May 2003 10:17:19 -0400


On Tue, May 13, 2003, Raymond Hettinger wrote:
>
> Was there a reason that __slots__ makes initialized variables
> read-only?  It would be useful to have overridable default values
> (even if it entailed copying them into an instance's slots):
>
> class Pane(object):
>     __slots__ = ('background', 'foreground', 'size', 'content')
>     background = 'black'
>     foreground = 'white'
>     size = (80, 25)
> 
> p = Pane()
> p.background = 'light blue'     # override the default
> assert p.foreground == 'white' # other defaults still in-place

Why not do the initializing in __init__?
-- 
Aahz (aahz@pythoncraft.com)           <*>         http://www.pythoncraft.com/

"In many ways, it's a dull language, borrowing solid old concepts from
many other languages & styles:  boring syntax, unsurprising semantics,
few automatic coercions, etc etc.  But that's one of the things I like
about it."  --Tim Peters on Python, 16 Sep 93