[Python-checkins] bpo-12634: Clarify an awkward section of the tutorial (GH-15406) (GH-15409)
Raymond Hettinger
webhook-mailer at python.org
Fri Aug 23 02:46:00 EDT 2019
https://github.com/python/cpython/commit/f6a7f5bc50f4267cfb7fe0c4ea16d5359c009cbd
commit: f6a7f5bc50f4267cfb7fe0c4ea16d5359c009cbd
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Raymond Hettinger <rhettinger at users.noreply.github.com>
date: 2019-08-22T23:45:57-07:00
summary:
bpo-12634: Clarify an awkward section of the tutorial (GH-15406) (GH-15409)
(cherry picked from commit 483ae0cf1dcf46f8b71c4bf32419dd138e908553)
Co-authored-by: Raymond Hettinger <rhettinger at users.noreply.github.com>
files:
M Doc/tutorial/classes.rst
diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst
index 7619ccbc1f36..0c0dca99f21f 100644
--- a/Doc/tutorial/classes.rst
+++ b/Doc/tutorial/classes.rst
@@ -475,12 +475,20 @@ Random Remarks
.. These should perhaps be placed more carefully...
-Data attributes override method attributes with the same name; to avoid
-accidental name conflicts, which may cause hard-to-find bugs in large programs,
-it is wise to use some kind of convention that minimizes the chance of
-conflicts. Possible conventions include capitalizing method names, prefixing
-data attribute names with a small unique string (perhaps just an underscore), or
-using verbs for methods and nouns for data attributes.
+If the same attribute name occurs in both an instance and in a class,
+then attribute lookup prioritizes the instance::
+
+ >>> class Warehouse:
+ purpose = 'storage'
+ region = 'west'
+
+ >>> w1 = Warehouse()
+ >>> print(w1.purpose, w1.region)
+ storage west
+ >>> w2 = Warehouse()
+ >>> w2.region = 'east'
+ >>> print(w2.purpose, w2.region)
+ storage east
Data attributes may be referenced by methods as well as by ordinary users
("clients") of an object. In other words, classes are not usable to implement
More information about the Python-checkins
mailing list