NB: Example

In this example, we put our class code in a separate .py file.

Then we call it from within our notebook.

This is normal practice – class files cannot reside in notebooks if they are to be used by other code!

Open the file diegame.py in the course repo to inspect the code.

from diegame import DieGame
dg1 = DieGame()
dg2 = DieGame()
def check_values():
    print('n_rolls:', dg1.n_rolls, dg2.n_rolls, DieGame.n_rolls, sep='\n\t')
    print('weights:', dg1.weights, dg2.weights, DieGame.weights, sep='\n\t')
dg1.n_rolls = 20
dg1.weights = [1, 10, 1, 1, 1, 1]
dg2.weights.append(1) # Also alters the static because this is not an assignment
DieGame.n_rolls = 50
check_values()
n_rolls:
    20
    50
    50
weights:
    [1, 10, 1, 1, 1, 1]
    [1, 1, 1, 1, 1, 1, 1]
    [1, 1, 1, 1, 1, 1, 1]