Skip to content

Commit f6a7f5b

Browse files
bpo-12634: Clarify an awkward section of the tutorial (GH-15406) (GH-15409)
(cherry picked from commit 483ae0c) Co-authored-by: Raymond Hettinger <[email protected]>
1 parent 4a40498 commit f6a7f5b

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

Doc/tutorial/classes.rst

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -475,12 +475,20 @@ Random Remarks
475475

476476
.. These should perhaps be placed more carefully...
477477
478-
Data attributes override method attributes with the same name; to avoid
479-
accidental name conflicts, which may cause hard-to-find bugs in large programs,
480-
it is wise to use some kind of convention that minimizes the chance of
481-
conflicts. Possible conventions include capitalizing method names, prefixing
482-
data attribute names with a small unique string (perhaps just an underscore), or
483-
using verbs for methods and nouns for data attributes.
478+
If the same attribute name occurs in both an instance and in a class,
479+
then attribute lookup prioritizes the instance::
480+
481+
>>> class Warehouse:
482+
purpose = 'storage'
483+
region = 'west'
484+
485+
>>> w1 = Warehouse()
486+
>>> print(w1.purpose, w1.region)
487+
storage west
488+
>>> w2 = Warehouse()
489+
>>> w2.region = 'east'
490+
>>> print(w2.purpose, w2.region)
491+
storage east
484492

485493
Data attributes may be referenced by methods as well as by ordinary users
486494
("clients") of an object. In other words, classes are not usable to implement

0 commit comments

Comments
 (0)