12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

mark = Person('ml', 'trainer')>>> dave = Person('da', 'developer')>>>>>> mark.job, dave.info( )('trainer', ('da', 'developer'))This scheme also makes multiple instances, but the class is not empty this time:we’ve added logic (methods) to initialize instances at construction time and collectattributes into a tuple. The constructor imposes some consistency on instances hereby always setting name and job attributes.We could further extend this code by adding logic to compute salaries, parse names,and so on (see the end of Chapter 24 for an example that does this). Ultimately, wemight link the class into a larger hierarchy to inherit an existing set of methods viathe automatic attribute search of classes, or perhaps even store instances of the classin a file with <strong>Python</strong> object pickling to make them persistent (more on pickling andpersistence in the sidebar “Why You Will Care: Classes and Persistence,” and againlater in the book). In the end, although types like dictionaries are flexible, classesallow us to add behavior to objects in ways that built-in types and simple functionsdo not directly support.OOP and Inheritance: “<strong>Is</strong>-a” RelationshipsWe’ve explored the mechanics of inheritance in depth already, but I’d like to showyou an example of how it can be used to model real-world relationships. From a programmer’spoint of view, inheritance is kicked off by attribute qualifications, whichtrigger searches for names in instances, their classes, and then any superclasses. Froma designer’s point of view, inheritance is a way to specify set membership: a classdefines a set of properties that may be inherited and customized by more specific sets(i.e., subclasses).To illustrate, let’s put that pizza-making robot we talked about at the start of thispart of the book to work. Suppose we’ve decided to explore alternative career pathsand open a pizza restaurant. One of the first things we’ll need to do is hire employeesto serve customers, prepare the food, and so on. Being engineers at heart, we’vedecided to build a robot to make the pizzas; but being politically and cyberneticallycorrect, we’ve also decided to make our robot a full-fledged employee with a salary.Our pizza shop team can be defined by the four classes in the example file,employees.py. The most general class, Employee, provides common behavior such asbumping up salaries (giveRaise) and printing (_ _repr_ _). There are two kinds ofemployees, and so two subclasses of Employee: Chef and Server. Both override theinherited work method to print more specific messages. Finally, our pizza robot ismodeled by an even more specific class: PizzaRobot is a kind of Chef, which is a kindOOP and Inheritance: “<strong>Is</strong>-a” Relationships | 521

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

Saved successfully!

Ooh no, something went wrong!