mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
bpo-12634: Clarify an awkward section of the tutorial (GH-15406) (GH-15409)
(cherry picked from commit 483ae0cf1d
)
Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
This commit is contained in:
parent
4a40498ea9
commit
f6a7f5bc50
1 changed files with 14 additions and 6 deletions
|
@ -475,12 +475,20 @@ Random Remarks
|
||||||
|
|
||||||
.. These should perhaps be placed more carefully...
|
.. These should perhaps be placed more carefully...
|
||||||
|
|
||||||
Data attributes override method attributes with the same name; to avoid
|
If the same attribute name occurs in both an instance and in a class,
|
||||||
accidental name conflicts, which may cause hard-to-find bugs in large programs,
|
then attribute lookup prioritizes the instance::
|
||||||
it is wise to use some kind of convention that minimizes the chance of
|
|
||||||
conflicts. Possible conventions include capitalizing method names, prefixing
|
>>> class Warehouse:
|
||||||
data attribute names with a small unique string (perhaps just an underscore), or
|
purpose = 'storage'
|
||||||
using verbs for methods and nouns for data attributes.
|
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
|
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
|
("clients") of an object. In other words, classes are not usable to implement
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue