A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2003-May/035575.html below:

[Python-Dev] __slots__ and default values

[Python-Dev] __slots__ and default values [Python-Dev] __slots__ and default valuesRaymond Hettinger Raymond Hettinger" <python@rcn.com
Tue, 13 May 2003 09:23:49 -0400
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



Raymond Hettinger
    




---------------------------

>>> class A(object):
 __slots__ = ('x',)
 x = 1
 
>>> class B(object):
 __slots__ = ('x',)

>>> A().x = 2
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in ?
    A().x = 2
AttributeError: 'A' object attribute 'x' is read-only
>>> B().x = 2



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4