Page 357 - Beginning Programming with Pyth - John Paul Mueller
P. 357

that wouldn't reflect the real world very well. To use this class in a real- world way for the rest of the chapter, you must follow these steps:
1. CreateanewnotebookcalledBPPD_15_MyClass.ipynb.
2. Click Run Cell.
Python will execute the code without error when you have typed the code correctly.
3. Choose File ⇒   Save and Checkpoint. Notebook saves the file.
4. Choose File ⇒   Download As ⇒   Python (.py). Notebook outputs the code as a Python file.
5. Import the resulting file into your Notebook.
The “Importing a notebook” section of Chapter 4 describes how to
perform this task.
Using the Class in an Application
Most of the time, you use external classes when working with Python. It isn’t very often that a class exists within the confines of the application file because the application would become large and unmanageable. In addition, reusing the class code in another application would be difficult. The following steps help you use the MyClass class that you created in the previous section.
1. Type the following code into the notebook for this chapter —
pressing Enter after each line:
import BPPD_15_MyClass
SamsRecord = BPPD_15_MyClass.MyClass() AmysRecord = BPPD_15_MyClass.MyClass("Amy", 44) print(SamsRecord.GetAge()) SamsRecord.SetAge(33) print(AmysRecord.GetName()) AmysRecord.SetName("Aimee")
print(SamsRecord)
print(AmysRecord)
  

















































































   355   356   357   358   359