Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
28137a09
Commit
28137a09
authored
May 20, 2003
by
Andrew M. Kuchling
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Don't mention __slots__ as a technique for error avoidance
parent
e960e225
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
6 additions
and
10 deletions
+6
-10
Doc/whatsnew/whatsnew22.tex
Doc/whatsnew/whatsnew22.tex
+6
-10
No files found.
Doc/whatsnew/whatsnew22.tex
View file @
28137a09
...
...
@@ -424,14 +424,9 @@ Finally, it's possible to constrain the list of attributes that can be
referenced on an object using the new
\member
{__
slots
__}
class attribute.
Python objects are usually very dynamic; at any time it's possible to
define a new attribute on an instance by just doing
\code
{
obj.new
_
attr=1
}
. This is flexible and convenient, but this
flexibility can also lead to bugs, as when you meant to write
\code
{
obj.template = 'a'
}
but made a typo and wrote
\code
{
obj.templtae
}
by accident.
A new-style class can define a class attribute named
\member
{__
slots
__}
to constrain the list of legal attribute names. An example will make
this clear:
\code
{
obj.new
_
attr=1
}
. A new-style class can define a class attribute named
\member
{__
slots
__}
to limit the legal attributes
to a particular set of names. An example will make this clear:
\begin{verbatim}
>>> class C(object):
...
...
@@ -443,16 +438,17 @@ None
>>> obj.template = 'Test'
>>> print obj.template
Test
>>> obj.
templtae
= None
>>> obj.
newattr
= None
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'C' object has no attribute '
templtae
'
AttributeError: 'C' object has no attribute '
newattr
'
\end{verbatim}
Note how you get an
\exception
{
AttributeError
}
on the attempt to
assign to an attribute not listed in
\member
{__
slots
__}
.
\subsection
{
Related Links
}
\label
{
sect-rellinks
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment