{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Workbook for Chapter 7 of Explorations in Computing"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Section 7.7: Defining New Objects: `class` Statements"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `Card` Class "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For the projects in this notebook you will be defining your own class. You should type the class definition in the code cell below.\n",
"\n",
"The first few exercises work with a trival class, and then later exercises have you add more and more features. Each time you update the class simply type ⌃⏎ in the code cell to execute the `class` statement.\n",
"\n",
"** Note: ** If you have any variables that are instances of the class they are not updated when the class statement is executed. You will have to re-execute the assignment statements that create objects if you want them to have the newest class definition."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class Card:\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Exercises"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T92. The first version of the Card class is in the code cell above. Type ⌃⏎ in that cell to define the class in this notebook session."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T94. If you call the built-in function named `dir` with no arguments Python will show you the contents of its \"whiteboard,\" the set of names defined for the current session:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T95. Define a variable named `x` to be a reference to a Card object:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T96. Ask Python to show you which class `x` belongs to:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T97. If you pass `x` to the `dir` function Python will show you the names defined in your object's namespace:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T98. Edit your class definition by entering the definition of the function named `__init__` shown on lines 3--4 of Figure 7.15. The name must begin and end with two underscore characters."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T99. Type ⌃⏎ in the code cell to update the definition of the Card class."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T100. Now when you make a Card object, you need to pass the card number to the constructor because your `__init__` function needs a value for the argument it calls `n`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T101. If you look at the names defined for this object you should see the card ID created by `__init__`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T102. Because `x` has a variable called `_id` you should be able to access it using a qualified name:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T103. Create another new Card object and ask to see its ID:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T104. Edit the definition of your Card class to add the `rank` and `suit` methods shown on lines 6--10 of Figure 7.15."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T105. Rerun the cell with the definition of the Card class and create a new object `x`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T106. Because card 51 is the ace of spades, it should have a rank equal to 12 and belong to suit 3:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T107. Make another card and ask for its rank and suit:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T108. Try creating some other Card objects and test the `rank` and `suit` methods. Do you see how cards 0 to 12 belong to suit 0, cards 13 to 25 belong to suit 1, and so on?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T109. Ask Python to print the values of two of the variables you defined:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T110. Add the definition of the `__repr__` method (Figure 7.16) to your class. Note there are two underscores at the beginning and end of the name."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T111. Rerun the cell with your class definition and create the two Card objects again:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T112. Now when you ask to see the values of `x` and `y` you should see strings created by `__repr__`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T113. Ask Python to compare your two Card objects:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T114. Add the definitions of the `__eq__` and `__lt__` functions from Figure 7.17 to your class, rerun the cell with the class definition, and create the same two objects again."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T115. Now when you compare two objects Python will use your definition of `__lt__` to compare the IDs of the two cards:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T116. Copy the `permute` function from the notebook for Section 7.5 into this code cell and run the cell to define the function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"** Note ** If you have not done so already add the definition of the `new_deck` function in Figure 7.15 to the code cell with your Card class and rerun the cell."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T117. Make a deck of cards using the `new_deck` function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T118. Shuffle the deck and print it:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T119. Because you told Python how to compare cards you should be able to sort the deck to put it back in order:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}