Categories
Computers

Some Kind of Pythonic Magic

In which I detail some of the things I’ve been enjoying in Python

I’ve been tinkering with my nascent Python “game”. Each time I expand on it, it’s to learn a bit more about Python, or some feature or module that seems appropriate. To that end, I’ve heard that Python comes with “batteries included”. Meaning the standard suite of tools that come with a typical install of Python is extensive.

To that end, it feels like the built in libraries are somewhat magical in how well they handle things. I’m comparing to C, whose idea of standard libraries feels unbelievably spartan in comparison. Different tools for different jobs, I suppose. There are really weird, simple pleasures in programming in Python. Consider:

import os from pathlib import Path scoreboard = Path(os.environ.get("XDG_DATA_HOME") + "/guessing-game/scores.txt") if scoreboard.exists: with scoreboard.open("a+") as scores: // Game code here

This is delightful. I don’t have to do stuff like count to see if the return from Path is greater than 1 to indicate existence of a file, like I have to in JS when selecting elements, Path comes with a ready-made exists method that does exactly what it says on the tin. Oh, and the whole thing figures out the path on POSIX vs Windows path styles. Scrumptious!

I know my little honeymoon phase with my incredibly basic script will probably come crashing down when I want to do stuff more complicated, like have a high-scores list, but I’m enjoying it while it lasts.

By Nathan

Nathan is a technologist and Open Source enthusiast living and working in Florida. Often, he can be found playing board games with his family, video games by himself or breaking technological things in order to fix them "better".