This repo contains a series of examples of strange Python behaviors. These are based on my remembrance of posts that I made while working at Meta that I thought would be fun to share.
Each folder (except experiments) corresponds to a post on lonfarr.com. A README.md contains a link to the blog post. Each of these folders is also referenced below in the table of contents with links.
The experiments
folder contains some smaller files that are little tests. Feel free to browse and view the README, but these files will not be as well maintained.
I'd highly recommend looking at the post on the website since it has additional information beyond what is in the code. This repo mainly serves as a place for the code to live and others to find it.
Since a fair number of the puzzles are exploring the realm of typing in Python, here is my setup for testing the code. I am using Pyright through the Pylance module in VSCode, but there are others such as Mypy and Pyre, which may yield different results. I personally find that the Pyright setup with VSCode is the easiest way to get started with type checking since it can all be installed easily in the IDE, so I opted for that configuration. To setup VSCode for strict type checking (which is my recommendation for any new code) you can add the following into the settings.json
for VSCode:
"python.analysis.typeCheckingMode": "strict",
I use black as my linter because it has no configuration, so it removes all of the minor lint debates (like spaces before and after equality operators). It is also easy to setup in VSCode. After installing the [black extension], I add the following to my settings.json
file so that it uses the formatter automatically on save:
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true
},
"black-formatter.showNotifications": "onWarning"
I'd like to call out several locations where I learned about these quirks or other useful tools.
- wtfpython GitHub
- A great GitHub (much greater than this one) with many examples. The readme is heavily detailed and you don't really even need to look at the code in the repo. A lot of my more detailed puzzles/examples were inspired by these relatively simple examples.
- mCoding YouTube Channel
- mCoding has a variety of videos on C++, Python, and Math. The Python videos do go into some depth of the inner workings of Python and led to some of the ideas for my posts.
- Carbon
- Allows you to create a sharable image of source code. Since some sites like LinkedIn do not have a good way to imbed code, to this is a nice option.
- Iterable Membership Check
- Iterators as Function Inputs
- How do you make a tuple?
- When Assert Does Not Raise
- Checking Against Infinity and NaN (not a number)
- Order of Operations and Chaining