How to do it...

  1. Import the cmd module:
        import cmd 
  1. Define an extension to cmd.Cmd:
        class Roulette(cmd.Cmd): 
  1. Define any initialization required in the preloop() method:
            def preloop(self): 
                self.bets = {} 
                self.stake = 100 
                self.wheel = wheel() 

This preloop() method is evaluated just once when the processing starts. We've used this to initialize a dictionary for bets and the player's stake. We also created an instance of the wheel collection. The self argument is a requirement for methods within a class. For now, it's a simply required syntax. In Chapter 6, Basics of Classes and Objects, we'll look at this more closely.

Note that this is indented within the class statement.

Initialization can also be done in the __init__() method. This ...

Get Modern Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.