RC27. The Walrus and the Char Pointer
Daily dispatches from my 12 weeks at the Recurse Center in Summer 2023
This short post has nothing to do with char
s or pointers (I couldn’t resist). It does, however, have to do with walruses – walrus operators, to be exact.
Like the Dall-e hallucination above, the walrus operator (:=
) looks … sort of walrus-y. It’s a thing I’ve known exists and have probably looked up and then forgotten about a dozen times. Yesterday it came up again while working on Implement DNS in a Weekend. Now I’m going to write about it, and by virtue of doing so I will never have to look the walrus operator up again.
The Walrus . . .
Normally, you might assign a variable in Python like so:
x = 10 # Step 1: Assign
and then evaluate it in a REPL like so:
x # Step 2: Evaluate
10
Or perhaps you prefer to assign and then print using the print()
function:
x = 15 # Step 1: Assign
print(x) # Step 2: Evaluate
15
The walrus operator allows you to assign and evaluate in a single step. That’s it.
(y := 100) # Steps 1 and 2: Assign and evaluate!
100
Or:
print(z := "O Oysters, come and walk with us!") # Assign and evaluate!
print(z)
O Oysters, come and walk with us!
O Oysters, come and walk with us!
. . . and the Char Pointer
I guess the reference to char
s and pointers is not wholy irreievalt, since the walrus operator turns out to enable very C-like syntax. In C, for instance, it is very natural to do something like this:
while ((c = getchar()) != EOF) {
// do stuff with `c`
}
In this case we’re assignining c
and evaluating the assignment expression in one go, so that c
can be be used as a component of the predicate c != EOF
.
While you would probably never read a file like this in Python, you could approximate this C-style syntax using the walrus:
while ((line := f.readline()) != '\n'):
print(line)
The sun was shining on the sea,
Shining with all his might:
He did his very best to make
The billows smooth and bright—
And this was odd, because it was
The middle of the night.
As another example, in C you could do things like this:
int i = 0;
char s[] = "The time has come";
printf("%c\n", s[i]); // s[0] => T
printf("%c\n", s[++i]); // s[1] => h
printf("%c\n", s[(i += 1)]); // s[2] => e
T
h
e
++i
and (i += 1)
both increment i
and evaluate to new i
in one go. You could walrus it up to accomplish a similar feat in Python:
s = "Of shoes and ships and sealing wax"
i = 0
print(s[i])
print(s[(i := i + 1)])
print(s[(i := i + 1)])
print(s[(i := i + 1)])
O
f
s
Other goings on
- Coffee-chat hat trick. Three damn fine coffee chats with a bunch of damn fine folks who are alums or in the last week of batch, all of whom offered some welcome perspective for someone at the halfway-point moment of reckoning. As a group the convos left me with the abiding sense that it’s okay not to build a React app or do whatever thing seems the most portfolio-ready.
- Continued on to Part 2 (which turned out to be Part 1 of Part 2) of the DNS project. Appreciating this introduction to all things DNS, which is piquing my curiosity about other network-y things.
- Spent a while redrawing CPU diagrams and talked CPU implementations at weekly nand2tetris meeting