class attribute
An attribute defined in the class body, shared by every instance of the class.
A shared mutable class attribute silently leaks data between instances.
In more detail
It suits values common to all instances, such as a species name. A mutable class attribute, such as a list, is shared too, so appending to it from one instance changes it for all, the shared mutable attribute trap; create per-instance lists in __init__ instead.
Related terms
- instance attribute — An attribute stored on one instance, usually set as self.name = value, so each instance has its own copy.
- mutable — Able to be changed in place after it is created, like most lists, arrays and objects in languages such as Python and JavaScript.
- aliasing — Having two or more names that refer to the same object, so a change made through one name shows up through the other.
Taught in
- Methods, Attributes And self — Hands-on Python: Complete Video Course & Book
- The Class That Shared One List — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Classes, Objects And Two Stacks — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Class Variables, Instance Variables And __dict__ — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Introspection: hasattr, __name__, __module__ And __bases__ — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Multiple Inheritance, The MRO And Diamonds — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Special Methods: __str__, __eq__ And Friends — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Object-Oriented Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Twelve Traps, Mostly Objects — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Mock, Review And Exam Day — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep