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.

chapter, these attributes really are dynamic in nature: you do not need to pre-declare or pre-assign<br />

them in the constructor or anywhere else.<br />

Methods<br />

One way we can improve our use of classes is to add functions to them. These class functions are known<br />

by their more common name, methods. In <strong>Python</strong>, methods are defined as part of the class definition,<br />

but can be invoked only on an instance. In other words, the path one must take to finally be able to call<br />

a method goes like this: (1) define the class (and the methods), (2) create an instance, and finally, (3)<br />

invoke the method on that instance. Here is an example class with a method:<br />

class MyDataWithMethod(object): # define the class<br />

def printFoo(self): # define the method<br />

print 'You invoked printFoo()!'<br />

You will notice the self argument, which must be present in all method declarations. That argument,<br />

representing the instance object, is passed to the method implicitly by the interpreter when you invoke<br />

a method on an instance, so you, yourself, do not have to worry about passing anything in (specifically<br />

self, which is automatically passed in for you).<br />

For example, if you have a method that takes two arguments, all of your calls should only pass in the<br />

second argument. <strong>Python</strong> passes in self for you as the first. If you make a mistake, do not worry about<br />

it. When an error occurs, <strong>Python</strong> will tell you that you have passed in the wrong number of arguments.<br />

You may make this mistake only once anyway... you'll certainly remember each time after that!<br />

The requirement of the instance (self) in each method's signature will be something new to those of<br />

you coming from C++ or Java, so be aware of that. It is all part of <strong>Python</strong>'s philosophy of being<br />

explicitly clear. In those other languages, self is called "this." You can find out more about self in the<br />

<strong>Core</strong> Note in Section 13.7 on page 540. Requiring the instance only applies to regular methods and not<br />

static or class methods, although the latter requires the class rather than the instance. You can find out<br />

more about static and class methods in Section 13.8 on page 542.<br />

Now we will instantiate the class and invoke the method once we have an instance:<br />

>>> myObj = MyDataWithMethod() # create the instance<br />

>>> myObj.printFoo() # now invoke the method<br />

You invoked printFoo()!<br />

We conclude this introductory section by giving you a slightly more complex example of what you can do<br />

with classes (and instances) and also introducing you to the special method __init__() as well as<br />

subclassing and inheritance.<br />

For those of you who are already familiar with object-oriented programming, __init__() is similar to the<br />

class constructor. If you are new to the world of OOP, a constructor is simply a special method that is<br />

typically called to create a new object. In <strong>Python</strong>, __init__() is not really the constructor. You do not<br />

call "new" to create a new object. (<strong>Python</strong> does not even have a keyword called "new" anyway.) Instead,<br />

<strong>Python</strong> creates the instance for you and then calls __init__() during instantiation to define additional<br />

behavior that should occur when a class is instantiated, i.e., setting up initial values or running some<br />

preliminary diagnostic codebasically performing any special tasks or setup after the instance is created<br />

but before the new instance is returned from the instantiation call.

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

Saved successfully!

Ooh no, something went wrong!