There is a lot of documentation on the web that explains the flow of exception handling in Python. The official documentation does a good job of it. But I have yet to find a good example use case for the
finally clause. If there are any Python experts out there who would be willing to show me a good use-case for
finally, I’d appreciate it. I’ll explain my confusion: Continue Reading »
Awhile ago, I traded emails with Christian Schramm about an article he had written called A Gentle Introduction to Generators in Python, which had been published on Tech.pro. Unfortunately, that site has since been taken down, at least temporarily: Continue Reading »
In this short video, I show you how Python
os.renames() method works: Continue Reading »
For Pygame and other graphics work, it’s helpful to have color constants that hold the color RGB values. I couldn’t find anything like this, so I created a color_constants module that:
- Contains constants for 551 named colors* (e.g, as named tuples:
Color = namedtuple('RGB','red, green, blue')
- Extends the
Color class to include a method for getting the hex formatted color:
- Stores these constants in an
The colors are shown in the table below and the full code is shown below that. Continue Reading »
UPDATE: Apparently, this is no longer necessary. As of February, 2017, you can just run
pip install pygame and it will install the right version of pygame for your environment. (Thanks, Chek Wei).
I’m not a game developer, but I’ve been playing with Pygame lately and have been impressed. I plan to use it in one of our Python courses to teach object-oriented programming. I use 64-bit Windows 8, so I have the 64-bit version of Python 3.4. Most of our students are unlikely to be game developers, but because games have obvious visible objects, they provide a nice framework for teaching OOP.
Unfortunately, Pygame doesn’t have an official 64-bit installer, so they recommend you use the 32-bit version of Python. So, I installed the 32-bit version of Python alongside the 64-bit version I already had, but that was a pain as I had to constantly make sure I was running the right one. It also made the 32-bit version the default.
Luckily, Christoph Gohlke of the University of California, Irvine has made a bunch of 64-bit binaries available for Python extension packages, including one for pygame. Continue Reading »
The Python documentation on
pow() states that
pow(x,y,z) is computed more efficiently than
pow(x,y) % z, but our tests don’t show that to be the case. In fact, we found
x**y % z to be the most efficient way to complete the operation. Note that this is academic as all the methods we tested for raising one number to the power of another and then doing a modulus operation are lightning fast. You should use whichever method suits your coding style the most, but for those who are curious, here’s how we tested it: Continue Reading »
Let’s say we have two scripts: foo.py and bar.py.
They are exactly the same. They begin with the definition of a function called
main(), which simply prints out the value of
__name__, a built-in variable that returns the name of the module. Continue Reading »
The difference between
str.isdecimal() in Python is, for most of us, academic and can be used interchangeably. According to the Python documentation, the decimal category “includes digit characters, and all characters that can be used to form decimal-radix numbers, e.g. U+0660, ARABIC-INDIC DIGIT ZERO.” Continue Reading »