Online Book Reader

Home Category

Learning Python - Mark Lutz [445]

By Root 1364 0
should inherit basic method behavior from MyList. Adding a sequence to a MyListSub should print a message, increment the counter for + calls, and perform the superclass’s method. Also, introduce a new method that prints the operation counters to stdout, and experiment with your class interactively. Do your counters count calls per instance, or per class (for all instances of the class)? How would you program the other option)? (Hint: it depends on which object the count members are assigned to: class members are shared by instances, but self members are per-instance data.)

Attribute methods. Write a class called Meta with methods that intercept every attribute qualification (both fetches and assignments), and print messages listing their arguments to stdout. Create a Meta instance, and experiment with qualifying it interactively. What happens when you try to use the instance in expressions? Try adding, indexing, and slicing the instance of your class. (Note: a fully generic approach based upon __getattr__ will work in 2.6 but not 3.0, for reasons noted in Chapter 30 and restated in the solution to this exercise.)

Set objects. Experiment with the set class described in Extending Types by Embedding. Run commands to do the following sorts of operations: Create two sets of integers, and compute their intersection and union by using & and | operator expressions.

Create a set from a string, and experiment with indexing your set. Which methods in the class are called?

Try iterating through the items in your string set using a for loop. Which methods run this time?

Try computing the intersection and union of your string set and a simple Python string. Does it work?

Now, extend your set by subclassing to handle arbitrarily many operands using the *args argument form. (Hint: see the function versions of these algorithms in Chapter 18.) Compute intersections and unions of multiple operands with your set subclass. How can you intersect three or more sets, given that & has only two sides?

How would you go about emulating other list operations in the set class? (Hint: __add__ can catch concatenation, and __getattr__ can pass most list method calls to the wrapped list.)

Class tree links. In Namespaces: The Whole Story in Chapter 28 and in Multiple Inheritance: “Mix-in” Classes in Chapter 30, I mentioned that classes have a __bases__ attribute that returns a tuple of their superclass objects (the ones listed in parentheses in the class header). Use __bases__ to extend the lister.py mix-in classes we wrote in Chapter 30 so that they print the names of the immediate superclasses of the instance’s class. When you’re done, the first line of the string representation should look like this (your address may vary):

Composition. Simulate a fast-food ordering scenario by defining four classes: Lunch

A container and controller class

Customer

The actor who buys food

Employee

The actor from whom a customer orders

Food

What the customer buys

To get you started, here are the classes and methods you’ll be defining:class Lunch:

def __init__(self) # Make/embed Customer and Employee

def order(self, foodName) # Start a Customer order simulation

def result(self) # Ask the Customer what Food it has

class Customer:

def __init__(self) # Initialize my food to None

def placeOrder(self, foodName, employee) # Place order with an Employee

def printFood(self) # Print the name of my food

class Employee:

def takeOrder(self, foodName) # Return a Food, with requested name

class Food:

def __init__(self, name) # Store food name

The order simulation should work as follows: The Lunch class’s constructor should make and embed an instance of Customer and an instance of Employee, and it should export a method called order. When called, this order method should ask the Customer to place an order by calling its placeOrder method. The Customer’s placeOrder method should in turn ask the Employee object for a new Food object by calling Employee’s takeOrder method.

Food objects

Return Main Page Previous Page Next Page

®Online Book Reader