15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

13.4. Class Attributes<br />

What is an attribute? An attribute is a data or functional element that belongs to another object and is<br />

accessed via the familiar dotted-attribute notation. Some <strong>Python</strong> types such as complex numbers have<br />

data attributes (real and imag), while others such as lists and dictionaries have methods (functional<br />

attributes).<br />

One interesting side note about attributes is that when you are accessing an attribute, it is also an<br />

object and may have attributes of its own which you can then access, leading to a chain of attributes, i.<br />

e., myThing.subThing.subSubThing, etc. Some familiar examples are:<br />

● sys.stdout.write('foo')<br />

● print myModule.myClass.__doc__<br />

● myList.extend(map(upper, open('x').readlines()))<br />

Class attributes are tied only to the classes in which they are defined, and since instance objects are the<br />

most commonly used objects in everyday OOP, instance data attributes are the primary data attributes<br />

you will be using. Class data attributes are useful only when a more "static" data type is required which<br />

is independent of any instances, hence the reason we are making the next section advanced, optional<br />

reading. (If you are unfamiliar with static, it just means a value that hangs around a function for each<br />

call, or a piece of data in a class that is the same across all instances. More about static data in the next<br />

subsection.)<br />

In the succeeding subsection, we will briefly describe how methods in <strong>Python</strong> are implemented and<br />

invoked. In general, all methods in <strong>Python</strong> have the same restriction: they require an instance before<br />

they can be called.<br />

13.4.1. Class Data Attributes<br />

Data attributes are simply variables of the class we are defining. They can be used like any other<br />

variable in that they are set when the class is created and can be updated either by methods within the<br />

class or elsewhere in the main part of the program.<br />

Such attributes are better known to OO programmers as static members, class variables, or static data.<br />

They represent data that is tied to the class object they belong to and are independent of any class<br />

instances. If you are a Java or C++ programmer, this type of data is the same as placing the static<br />

keyword in front of a variable declaration.<br />

Static members are generally used only to track values associated with classes. In most circumstances,<br />

you would be using instance attributes rather than class attributes. We will compare the differences<br />

between class and instance attributes when we formally introduce instances.<br />

Here is an example of using a class data attribute (foo):<br />

>>> class C(object):<br />

... foo = 100<br />

>>> print C.foo

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!