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.6. Instance Attributes<br />

Instances have only data attributes (methods are strictly class attributes) and are simply data values<br />

that you want to be associated with a particular instance of any class and are accessible via the familiar<br />

dotted-attribute notation. These values are independent of any other instance or of the class it was<br />

instantiated from. When an instance is deallocated, so are its attributes.<br />

13.6.1. "Instantiating" Instance Attributes (or Creating a Better Constructor)<br />

Instance attributes can be set any time after an instance has been created, in any piece of code that has<br />

access to the instance. However, one of the key places where such attributes are set is in the<br />

constructor, __init__().<br />

<strong>Core</strong> Note: Instance attributes<br />

Being able to create an instance attribute "on-the-fly" is one of the<br />

great features of <strong>Python</strong> classes, initially (but gently) shocking those<br />

coming from C++ or Java in which all attributes must be explicitly<br />

defined/ declared first.<br />

<strong>Python</strong> is not only dynamically typed but also allows for such dynamic<br />

creation of object attributes during run-time. It is a feature that once<br />

used may be difficult to live without. Of course, we should mention to<br />

the reader that one much be cautious when creating such attributes.<br />

One pitfall is when such attributes are created in conditional clauses: if<br />

you attempt to access such an attribute later on in your code, that<br />

attribute may not exist if the flow had not entered that conditional<br />

suite. The moral of the story is that <strong>Python</strong> gives you a new feature<br />

you were not used to before, but if you use it, you need to be more<br />

careful, too.<br />

Constructor First Place to Set Instance Attributes<br />

The constructor is the earliest place that instance attributes can be set because __init__() is the first<br />

method called after instance objects have been created. There is no earlier opportunity to set instance<br />

attributes. Once __init__() has finished execution, the instance object is returned, completing the<br />

instantiation process.<br />

Default Arguments Provide Default Instance Setup<br />

One can also use __init__() along with default arguments to provide an effective way of preparing an<br />

instance for use in the real world. In many situations, the default values represent the most common<br />

cases for setting up instance attributes, and such use of default values precludes them from having to<br />

be given explicitly to the constructor. We also outlined some of the general benefits of default

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

Saved successfully!

Ooh no, something went wrong!