File tree Expand file tree Collapse file tree 1 file changed +14
-6
lines changed Expand file tree Collapse file tree 1 file changed +14
-6
lines changed Original file line number Diff line number Diff line change @@ -475,12 +475,20 @@ Random Remarks
475
475
476
476
.. These should perhaps be placed more carefully...
477
477
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
484
492
485
493
Data attributes may be referenced by methods as well as by ordinary users
486
494
("clients") of an object. In other words, classes are not usable to implement
You can’t perform that action at this time.
0 commit comments