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.

if _ _name_ _ == '_ _main_ _':bob = Person('Bob Smith', 40)print bobprint bob.lastName( )bob.birthDay( )print bobsue = Employee('Sue Jones', 44, job='dev', pay=100000)print sueprint sue.lastName( )sue.birthDay( )sue.giveRaise(.10)print sueTo test the code, we can import the module and make instances interactively. Here,for example, is the Person class in action. Creating an instance triggers _ _init_ _,calling a named method uses or changes instance state information (attributes), andprinting an instance invokes the inherited _ _str_ _ to print all attributes generically:>>> from person import Person>>> ann = Person('Ann Smith', 45)>>> ann.lastName( )'Smith'>>> ann.birthDay( )>>> ann.age46>>> print annFinally, here is the output of the file’s self-test logic (the code at the bottom, underthe _ _name_ _ test), which creates a person and an employee, and changes each ofthem. As usual, this self-test code is run only when the file is run as a top-level script,not when it is being imported as a library module. Notice how employees inheritprint formats and last-name extraction, have more state information, have an extramethod for getting a raise, and run a customized version of the birthday method(they age by two!):% python person.pySmith514 | Chapter 24: Class Coding Details

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

Saved successfully!

Ooh no, something went wrong!