Sign in

Ned Batchelder

@nedbat.com
4.4K followers 171 following 232 posts

Python, Boston, mathy fun, juggling, autism parenting. nedbat.com

PostsRepliesMedia
Ned Batchelder @nedbat.com · 25/10/2025
I'm writing letters to defend democracy, how are you spending your day? votefwd.org
A stack of stamped envelopes, ready for mailing
1120
Ned Batchelder @nedbat.com · 07/08/2025
Every puzzle contains another puzzle: how was it constructed? The game Flow Free asks you to find the paths to connect same-colored dots on a grid. How are these layouts created?
A hexagonal grid with tightly-packed paths connecting same-colored dots.
261
Ned Batchelder @nedbat.com · 02/04/2025
Want a concise daily digest of issue and pull request activity from your GitHub repos? Try Dinghy: pypi.org/project/ding...
091
Ned Batchelder @nedbat.com · 25/03/2025
My watchgha lets you watch the progress of GitHub Actions in your terminal. Now the job names are sorted, since GitHub now returns them in a randomish order. github.com/nedbat/watch...
3272
Ned Batchelder @nedbat.com · 18/01/2025
In #Python, @classmethod can used to create alternate constructors for making objects in other ways. Here's a simple example:
An example class called Pet with an alternate constructor.  Full text at https://gist.github.com/nedbat/b83352b2e20e0dc297ea99a37e172a8c
4533
Ned Batchelder @nedbat.com · 13/01/2025
Comparison operators in #Python can be chained together, but using them that way could be controversial. I wrote this this other day:
Checking two variable are equal and not zero: n_passed == n_tests != 0.  Full text at https://gist.github.com/nedbat/6f92fe29bdb47c0a36be0d29f5dd92ae
4272
Ned Batchelder @nedbat.com · 09/01/2025
Years ago, I tried ads on my site. My business card cube page got some attention, so I had $200 revenue for the year. When my tax guy saw that, he said, "Why don't you just make 1000 sites?" He was ahead of his time... nedbatchelder.com/text/cardcub...
Visual steps to assemble a six-card cube.
1140
Ned Batchelder @nedbat.com · 08/01/2025
Wow, Apple somehow added a notification with a circle-x close button that literally does not work, and the thing appears on every video call:
"Turn On Reactions" pop-up: "Google Chrome has turned off reaction effects..." etc.
040
Ned Batchelder @nedbat.com · 31/12/2024
Happy #Python New Year!
Printing "Happy New Year 2025" with two different sums and powers using range(10).  Full text at https://gist.github.com/nedbat/7eb9222bf0d3466f2bbf6381ccaf89ec
3486
Ned Batchelder @nedbat.com · 12/12/2024
Is this #Python too compact? Perhaps, but there could be useful things to learn. Full explanation unpacked at nedbatchelder.com/blog/201802/...
A Python function to unpack ranges like "1-5,7,9,10-13" into a list of numbers.  Full text at https://gist.github.com/nedbat/01fe96aac4a7d35bbd95e0fe9d5c6649
11524
Ned Batchelder @nedbat.com · 11/12/2024
After I posted, I saw that the browser has to be nearly full-width on my laptop for it to display.
110
Ned Batchelder @nedbat.com · 06/12/2024
Splitting a string once in #Python: .split() has maxsplit=, but .partition() can be better:
Demonstrating how to split a string just once with .split(maxsplit=), and with .partition().  Full text at https://gist.github.com/nedbat/93ff430f32231361ad0910433cc2c430
2629
Ned Batchelder @nedbat.com · 04/12/2024
I got a lot of replies with suggestions, so here are more #Python expressions to check if a (possibly empty) string has only 0's and 1's in it:
More Python expressions.  Full text at https://gist.github.com/nedbat/d224e99e3b88a6484715cc5318e2b104
3161
Ned Batchelder @nedbat.com · 01/12/2024
You can write short powerful #Python expressions using its many tools. Are there other ways to check if a string contains only "0" or "1"?
Six different Python checks of a string. Full text at https://gist.github.com/nedbat/5e7c0874df2134f101e8331d3f7c6d5c
14539
Ned Batchelder @nedbat.com · 26/11/2024
Are you using #Python 3.11 yet? It's been out for two years! Maybe this will entice you: better error messages!
Showing 3.10 and 3.11 with the same error.  3.11 pinpoints the problem.  Full text at https://gist.github.com/3a186827ac8bdfa08f34d36b9fce48dc
37212
Ned Batchelder @nedbat.com · 24/11/2024
Classes in #Python can define their own formatting mini-languages for f-strings by defining __format__. Full details: nedbatchelder.com/blog/202204/...
A latitude/longitude class that can be formatted in various ways.  Full text at https://gist.github.com/nedbat/4deeb3c4277bb29bf55bf5fb1d11b33e
010221
Ned Batchelder @nedbat.com · 23/11/2024
But "charging" is still black/white with a symbol. Why take yellow from iPhone and not the rest of the design (green for charging)? This just seems dumb.
A macOS menu bar showing the battery charging with a lightning bolt symbol.
130
Ned Batchelder @nedbat.com · 23/11/2024
Who at Apple thought low-power mode in Sequoia should be represented by yellow in the all-monochrome menu bar? Yellow means low power (a good thing)? Yellow on white? wtf?
Part of the macOS menu bar with the battery level indicated by a yellow-filled rectangle.
151
Ned Batchelder @nedbat.com · 22/11/2024
What if you want space around the word before centering? Is #Python letting me take this too far? Nested f-strings!?
Adding spaces around a word before centering it in equals signs.  Full text at https://gist.github.com/nedbat/fa35b505438146816851f9836fd2593c
382
Ned Batchelder @nedbat.com · 21/11/2024
Also: you can also use variables (or expressions?!) for the formatting information! #Python is cool... More details and explanation at fstring.help
Showing using variables for the character and width specification in the format.  Full text at https://gist.github.com/nedbat/db70d4cc6f16e88a33889ec113dcbe4d
2366
Ned Batchelder @nedbat.com · 21/11/2024
String formatting in #Python lets you left-, right-, or center-justify a value. You can specify the fill character to use. The syntax is colon, fill-char, <^>, width:
Demonstrating string formatting with right-, left-, and center-aligned text and custom fill characters.  Full text at https://gist.github.com/nedbat/9a29a7e0f8091844d65f5ca433612e59
611912
Ned Batchelder @nedbat.com · 17/11/2024
For loops in #Python can assign to more than just plain names. Any assignment target is allowed:
Using `for params["page"] in ...` to move through paged results in a search API.  Full text at https://gist.github.com/nedbat/d04b7d1bef5fb84542f23126424c814c
7679
Ned Batchelder @nedbat.com · 11/11/2024
#Python primitives are powerful tools that can combine to get a lot done with just a little code. The dict() constructor can make a dict from key/value pairs:
dict() takes pairs, and .split() can make pairs. Combine them to parse a string of options. Full text at https://gist.github.com/0e053c8c23de3e6bc83727d3cd94240e
0111
Ned Batchelder @nedbat.com · 04/11/2024
Nested loops can sometimes be replaced by one itertools.product() from the #Python stdlib:
Examples of using itertools.product, including a graphic output of occurrences of dice totals.  Full text at https://gist.github.com/nedbat/92eca8c3edd1066e366c46817653ba78
2443
Ned Batchelder @nedbat.com · 29/10/2024
In #Python, dicts can have tuples as keys. Consider a dict with (x,y) keys instead of nested lists for a grid. It simplifies sparse grids, "resizes" are automatic, it doesn't matter where (0,0) is, and you can use negative positions:
A dict with (x,y) keys, printed out as a 2D board. Full text at https://gist.github.com/10c1f9a412abbd1edd21c4e55bddc5ebMore added to the board, without worrying about size or position. Full text at https://gist.github.com/nedbat/10c1f9a412abbd1edd21c4e55bddc5eb
1143
Ned Batchelder @nedbat.com · 26/10/2024
"WAIT: Why am I talking" #PyCon #PyConAPAC #python by hachyderm.io/@georgically... hachyderm.io/@pypodcats@f...
A flowchart entitled "WAIT: Why Am I Talking" that encourages you to not talk.
092
Ned Batchelder @nedbat.com · 23/10/2024
Be careful in #Python when trying to compare object methods... "Bound method" objects are made anew each time, and reference the object and the function.
Python example of comparing object methods that doesn't work as you expect.  Full text at https://gist.github.com/nedbat/6895cbc5754ec7166ac074430f48de71
172
Ned Batchelder @nedbat.com · 17/09/2024
A list of commits is not a changelog.
The Simpsons "Don't make me tap the sign" meme.  The sign reads, "A list of commits is not a changelog."
040
Ned Batchelder @nedbat.com · 30/11/2023
Mutable default values in #Python surprise people, but it's right there in the name: value, not expression. nedbatchelder.com/blog/202311/...
080