]> granicus.if.org Git - python/commitdiff
bpo-12634: Clarify an awkward section of the tutorial (GH-15406)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Fri, 23 Aug 2019 06:27:04 +0000 (23:27 -0700)
committerGitHub <noreply@github.com>
Fri, 23 Aug 2019 06:27:04 +0000 (23:27 -0700)
Doc/tutorial/classes.rst

index 7619ccbc1f36e9144fe045f4f5de85a6f6e1a370..0c0dca99f21f286fe600392378d7121b0c917423 100644 (file)
@@ -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