housecarpenter: A drawing of Bob Dylan singing into a microphone with white make-up on his face. (Default)


I was wondering what the name was of the lullaby I used to listen to to go to sleep when I was little (I used to have a blue plastic rabbit with a string hanging off it, which I'd yank to make it play the tune.) Turns out it's a Brahms composition.

Now if only I could find the name of the melody the ice cream vans used to play...

(Actually, it might have just been Greensleeves. I feel like there might have been another melody other than Greensleeves I heard a lot, but I might be imagining it.)
housecarpenter: A drawing of Bob Dylan singing into a microphone with white make-up on his face. (Default)
Today in new life experiences: I was shat on by a pigeon.
housecarpenter: A drawing of Bob Dylan singing into a microphone with white make-up on his face. (Default)
I fixed a bug today which was mildly interesting. It was caused by a local variable in a function having the same name as an unrelated imported module which was referenced earlier in the function. So the code was of this shape:

import x

...

def f():
    x.g()

    ...

    x = a


When g was called, an error occurred with the message "local variable x referenced before assignment". Now when I was debugging this, all I saw at first was the x.g() line (since that's the line the error occurred at), and I scrolled up, trying to find where the x variable had been defined, eventually finding that x was the name of the module. This left me rather confused, since I couldn't imagine how the module name x was getting interpreted as the name of a local variable when it hadn't been reassigned at any point earlier in the code.

Of course, the answer is that the later reassignment was responsible. Python (like most programming languages, I believe) allocates local variables on the call stack, so it has to know what local variables there are as it calls the function, before it executes the function body. So it effectively does an initial pass over the body looking for assignments, and notices the x = a line and rebinds the name x to a local variable, before it executes the x.g() line.

Now I see why C89 requires you to put all variable declarations at the start of a function! OK, I don't know if this was ever the actual rationale behind that restriction, but it does seem like a good reason for it. The C89 restriction makes it clear that local variable names have to be valid across the whole body of a function, not just from the line on which they are declared onwards.
housecarpenter: A drawing of Bob Dylan singing into a microphone with white make-up on his face. (Default)


One of my dad's co-workers gave me a bunch of bird field guides from the 1970s!

This is pretty much the perfect Christmas gift. And it's from somebody I don't even know :) (although I guess my dad has been telling him about my interests.)

Profile

housecarpenter: A drawing of Bob Dylan singing into a microphone with white make-up on his face. (Default)
housecarpenter

April 2019

S M T W T F S
 123456
78910111213
14151617181920
21222324252627
2829 30    

Syndicate

RSS Atom

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Apr. 23rd, 2025 01:15 pm
Powered by Dreamwidth Studios