15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

arguments in Section 11.5.2. One caveat is that default arguments should be immutable objects;<br />

mutable objects like lists and dictionaries act like static data, maintaining their contents with each<br />

method call.<br />

Example 13.1 shows how we can use the default constructor behavior to help us calculate some sample<br />

total room costs for lodging at hotels in some of America's large metropolitan areas.<br />

Example 13.1. Using Default Arguments with Instantiation (hotel.py)<br />

Class definition for a fictitious hotel room rate calculator. The __init__() constructor<br />

method initializes several instance attributes. A calcTotal() method is used to determine<br />

either a total daily room rate or the total room cost for an entire stay.<br />

1 class HotelRoomCalc(object):<br />

2 'Hotel room rate calculator'<br />

3<br />

4 def __init__(self, rt, sales=0.085, rm=0.1):<br />

5 '''HotelRoomCalc default arguments:<br />

6 sales tax == 8.5% and room tax == 10%'''<br />

7 self.salesTax = sales<br />

8 self.roomTax = rm<br />

9 self.roomRate = rt<br />

10<br />

11 def calcTotal(self, days=1):<br />

12 'Calculate total; default to daily rate'<br />

13 daily = round((self.roomRate *<br />

14 (1 + self.roomTax + self.salesTax)), 2)<br />

15 return float(days) * daily<br />

The main purpose of our code is to help someone figure out the daily hotel room rate, including any<br />

state sales and room taxes. The default is for the general area around San Francisco, which has an<br />

8.5% sales tax and a 10% room tax. The daily room rate has no default value, thus it is required for any<br />

instance to be created.<br />

The setup work is done after instantiation by __init__() in lines 4-8, and the other core part of our code<br />

is the calcTotal() method, lines 10-14. The job of __init__() is to set the values needed to determine<br />

the total base room rate of a hotel room (not counting room service, phone calls, or other incidental<br />

items). calcTotal() is then used to either determine the total daily rate or the cost of an entire stay if<br />

the number of days is provided. The round() built-in function is used to round the calculation to the<br />

closest penny (two decimal places). Here is some sample usage of this class:<br />

>>> sfo = HotelRoomCalc(299) # new instance<br />

>>> sfo.calcTotal() # daily rate<br />

354.32<br />

>>> sfo.calcTotal(2) # 2-day rate<br />

708.64<br />

>>> sea = HotelRoomCalc(189, 0.086, 0.058) # new instance<br />

>>> sea.calcTotal()<br />

216.22<br />

>>> sea.calcTotal(4)<br />

864.88<br />

>>> wasWkDay = HotelRoomCalc(169, 0.045, 0.02) # new instance

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

Saved successfully!

Ooh no, something went wrong!