Access class attribute when object ofthe class exists
pythonhtp1_07.fm Page 255 Saturday, December 8, 2001 2:29 PM | ![]() |
|||
---|---|---|---|---|
Chapter 7 |
• To create an object of a class, simply “call” the class name as if it were a function. This call invokes the constructor for the class.
• Classes and objects of classes both have special attributes that can be manipulated. These at-tributes, which Python creates when a class is defined or when an object of a class is created, pro-vide information about the class or object of a class to which they belong.
|
||
---|---|---|
• When a class defines access methods, a client should access an object’s attributes only through | ||
|
||
• Classes often provide methods that allow clients to set or get the values of attributes. Although | ||
• When a class author creates an attribute with a single leading underscore, the author does not want users of the class to access the attribute directly. If a program requires access to the attributes, the class author provides some other means for doing so.
• Python comparisons may be chained. The chaining syntax that enables programmers to write com- parison expressions in familiar, arithmetic terms.
• To prevent indiscriminate attribute access, prefix the name of the attribute with two underscore | ||
---|---|---|
|
||
![]() |
![]() |
![]() |
||||
---|---|---|---|---|
256 | Chapter 7 |
• When Python encounters an attribute name that begins with two underscores, the interpreter per-forms name mangling on the attribute, to prevent indiscriminate access to the data. Name man-gling changes the name of an attribute by including information about the class to which the attribute belongs.
• Constructors can define default arguments that specify initial values for an object’s attributes, if the client does not specify an argument at construction time.
• A class can define a special method called __del__ that executes when the last reference to an object is deleted or goes out of scope. A destructor normally specifies no parameters other than self and returns None.
• A class attribute represents “class-wide” information (i.e., a property of the class, not of a specific | ||
---|---|---|
• Although class attributes may seem like global variables, each class attribute resides in the | ||
• A class’s class attributes can be accessed through any object of that class. A class’s class attributes | ||
|
• Sometimes, a programmer needs objects whose attributes are themselves references to objects of other classes. Such a capability is called composition.
• A queue returns items in first-in, first-out (FIFO) order, which means that the first item inserted in a queue is the first item removed.
• Python programmers concentrate both on crafting new classes and on reusing classes from the standard library. This kind of software reusability speeds the development of powerful, high-qual-ity software.
• The standard library enables Python developers to build applications faster by reusing preexisting, | ||
---|---|---|
![]() |
![]() |