16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

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.

174 CHAPTER 9 ■ MAGIC METHODS, PROPERTIES, AND ITERATORS<br />

class NewStyle(object):<br />

more_code_here<br />

class OldStyle:<br />

more_code_here<br />

Of these two, NewStyle is a new-style class, while OldStyle is an old-style class.<br />

■Note Rather than subclass a built-in type, you can make sure you’re using the same metaclass as they<br />

do. Metaclasses are the classes of other classes (or types)—a rather advanced <strong>to</strong>pic. However, you can use<br />

the built-in metaclass called type <strong>to</strong> create new-style classes rather easily. Either you put the following<br />

assignment in the <strong>to</strong>p-level scope of your module (close <strong>to</strong> the <strong>to</strong>p), or in the class-scope of your class:<br />

__metaclass__ = type<br />

Putting it at the beginning of a module makes it easy <strong>to</strong> make all your classes new-style. For more information<br />

on metaclasses, you can take a look at the (somewhat technical) article called “Unifying types and classes in<br />

<strong>Python</strong> 2.2” by Guido van Rossum (http://python.org/2.2/descrintro.html), or you can do a Web<br />

search for the term “python metaclasses.”<br />

In this book, I have taken the conservative approach of subclassing object only where it is<br />

needed (because object did not exist before version 2.2), but if you do not specifically have <strong>to</strong><br />

make your programs compatible with old versions of <strong>Python</strong>, I would advise you <strong>to</strong> make all<br />

your classes new-style, and consistently use features such as the super function (described in<br />

the section “Using the super Function,” later in this chapter).<br />

Construc<strong>to</strong>rs<br />

The first magic method we’ll take a look at is the construc<strong>to</strong>r. In case you have never heard the<br />

word “construc<strong>to</strong>r” before, it’s basically a fancy name for the kind of initializing method I have<br />

already used in some of the examples, under the name init. What separates construc<strong>to</strong>rs from<br />

ordinary methods, however, is that the construc<strong>to</strong>rs are called au<strong>to</strong>matically right after an object<br />

has been created. Thus, instead of doing what I’ve been doing up until now:<br />

>>> f = FooBar()<br />

>>> f.init()<br />

construc<strong>to</strong>rs make it possible <strong>to</strong> simply do this:<br />

>>> f = FooBar()<br />

Creating construc<strong>to</strong>rs in <strong>Python</strong> is really easy; simply change the init method’s name<br />

from the plain old init <strong>to</strong> the magic version, __init__:

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

Saved successfully!

Ooh no, something went wrong!