Strange Coincidence
Great Stuff
Python not only offers generators with the yield
keyword; classes can also implement generators as iterators. For this the Pythonista defines two methods: __iter__()
and __next__()
. The quadruple underscores ("dunders") mark the official entry points for Python's standard library. If the Python interpreter sees a loop head like for n in Roulette()
, it instantiates an object of the Roulette
class, uses __iter__()
to access its iterator, and then retrieves new values from it with calls to __next__()
until the iterator throws an exception.
In the class defined in Listing 2, __iter__()
conveniently returns the Roulette
instance itself, because the class does not need a separate iterator, since it implements the iterator itself with __next__()
. The latter always returns a new random number in the range 0 to 36 and never throws an exception, so that the flow of the for
loop in the main program never stops.
Listing 2
roulette.py
01 #!/usr/bin/env python3 02 import random 03 04 class Roulette: 05 slots = 36 06 numbers = range(0,slots+1) 07 08 def __iter__(self): 09 return self 10 11 def __next__(self): 12 return self.__class__.numbers[random.randint(0, self.__class__.slots)]
The Roulette
class defines two class variables: slots
as the highest number on the roulette
wheel and numbers
as a sequence of numbers from 0 to 36 inclusive. It makes sense to define the variables once only for the class and not to rebuild them for each instance or even every time the iterator is called.
Class or Instance?
Python's class variables differ from instance variables in that they are not accessed with self.variable
, but with __class__.variable
or self.__class__.variable
. For read-only access, you could even reference the class variable using the self.variable
instance path.
But if you modify the latter, you may be in for a surprise, because Python creates a new instance variable behind your back. The instance variable will be decoupled from the class variable so that each object will modify its own, instead of propagating changes to the class level. Also, methods do not find the class variable simply by its name; if you simply reference slots
in __iter__()
, you can expect the syntax checker to blow up in your face.
As so often in the Python world, there is a small but subtle difference between versions 2 and 3: The iterator entry into the generator class goes by the name of next
in Python 2 and not __next__
as in Python 3; programmers who want to use both versions thus typically simply define another next()
method, which passes the parameters fed to it to __next__()
without modification. Python 3 does not use next()
, so the compatibility trick does no harm there.
The output of the statistical evaluation of the roulette generator is shown in Listing 3. After 27 rounds, a doublet appeared for the first time: the number 11 occurred twice in a row. After 6,249 rounds of Faites vos jeux, 15 occurred three times in a row; after 57,393 games, 34 occurred four times in a row, and so on.
Listing 3
roulette-run
1 $ ./roulette 2 max_run: 2 11 (27) 3 max_run: 3 15 (6249) 4 max_run: 4 34 (57393) 5 max_run: 5 1 (3363284) 6 max_run: 6 0 (95846456) 7 max_run: 7 26 (357289507)
Tumultuous Scenes
What would happen in Las Vegas at a roulette table if zero came up come six times in a row as in Listing 3 after 95 million rounds? Tumultuous scenes would probably take place in the casino before the pit boss appeared and sent the croupier home for the day, because every player at the table would immediately suspect that something fishy was going on. But, seen statistically, everything is above board; it's an inevitable fact that even random values will repeat at some time.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.