diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2019-08-23 06:27:04 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-23 06:27:04 (GMT) |
commit | 483ae0cf1dcf46f8b71c4bf32419dd138e908553 (patch) | |
tree | ac4b4cf9a0d4d8c56ab45f3999378ceacba76629 /Doc | |
parent | 657008ea0336ff4f275ed3f0c2b6dd2e52de2bba (diff) | |
download | cpython-483ae0cf1dcf46f8b71c4bf32419dd138e908553.zip cpython-483ae0cf1dcf46f8b71c4bf32419dd138e908553.tar.gz cpython-483ae0cf1dcf46f8b71c4bf32419dd138e908553.tar.bz2 |
bpo-12634: Clarify an awkward section of the tutorial (GH-15406)
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/tutorial/classes.rst | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst index 7619ccb..0c0dca9 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 |