Now I have a reason to use staticmethod

The python builtin staticmethod has been one of those language features that I understood, but couldn’t figure out why I would ever use it. When I did my PyOhio talk on decorators, I asked if anyone in the room could explain why to use it. People came up with these ideas:

  • Put associated functions inside the relevant class, so we don’t pollute the module namespace
  • Make java programmers feel less homesick

Both of these are valid, I guess, but in my mind, it just confirmed that I would never use staticmethod.

Then I watched this neat video. You don’t have to go watch that video before you read the next section. I just wanted to point to where I picked up this trick.

Remember that any function bound to a class gets called with an extra parameter inserted at the beginning. That parameter is a reference to the instance of the class. So, you can’t do this:

>>> def f(a, b):
... return a - b
...
>>> class C(object):
... pass
...
>>> C.f = f
>>> c = C()
>>> c.f(3, 1)
------------------------------------------------------------
Traceback (most recent call last):
File "", line 1, in
TypeError: f() takes exactly 2 arguments (3 given)

The call to f blew up because f only takes two parameters, but it got three parameters instead because python automatically adds on the self parameter at the beginning. So f really got called like this: f(self, 3, 1)

Now the reason for staticmethod becomes obvious. If I want to allow instances of my class C to call function f, I have to make f a staticmethod on C, so that the python plumbing won’t insert that extra first argument at the beginning.

So, I’ll overwrite the first C.f method with a static method, and then call c.f again:

>>> C.f = staticmethod(f)
>>> c.f(3, 1) # This is the same already-instantiated c from earlier.
2

As a side effect of watching that video, I think about dependency injection (DI) differently now. In the video, Thomas Woulters says he uses staticmethod for dependency injection. This kind of DI is a different approach than what I’ve seen before. Most everybody that talks about DI emphasizes passing in lots and lots of crap into the __init__ method or in any subsequent methods, so every component can be mocked out.

But it makes just as much sense to graft on dependencies to the class outside of the __init__ method too, since the language supports that. The end goal is still reached. Code can still tweak the dependencies and provide alternative objects, and at the same time, I don’t end up with ridiculously long function signatures.

Micro Center isn’t enough any more

So, I used to love going to Micro Center. I would always find a bunch of old books I wanted to read, or I could pick up a new cheap component for my crappy frankenstien desktop, or I could just wander and look at all the fancy new laptops. It was always a fun place to kill time.

Now it seems like I go there and then leave, empty-handed, in a worse mood than when I came in. I’m like a drug addict realizing I can’t get high any more no matter how much junk I take.

Today I went there while my wife bought a gift nearby at Babies R Us. First I was disappointed that they shrank the “discount books” wall down to just a tiny shelf. Then I asked and found out they didn’t have any GSM modem stuff. They didn’t have any swipe card readers or thumbprint scanners either. They didn’t even have the neat new open source router. Total waste of time.

Here’s what Micro Center does have:

  • Employees giving horrifying advice on how to remove spyware programs
  • Customers spending way too much time asking questions about printer toner compatibility
  • Classes on how to use Microsoft Word.
  • Lots and lots of probably bootleg DVDs.
  • “linux compatible” surge protectors and USB cables.
  • Nerd BO

If you need those things, go nuts. Anything else, just order it online.

F U NATURE. THIS MEANS WAR.

My wife looked out the kitchen window this morning and said I should come look and see it for myself.

There were about twenty cornstalks there last night. Each with fat delicious ears. And pumpkins in between them. It was beautiful. And in one night, something showed up and ravaged my corn patch. It wasn’t squirrels. Squirrels don’t have mouths that can strip a cob down like this.

Charlie and I talked about it. He thinks it was probably a lion that ate the corn. I’m not sure I agree. It might be a lion, but it might also be raccoons or possums or deer. You can’t really see it in this picture, but little ants are picking the last bits of goodness out of corn carcass.

This picture shows how whatever it was ate the stalks down to the ground in some places.

For maximum effect, it should have salted the earth, so that nothing would ever grow there again. Maybe it’s waiting for next year. Something similar happened last year.

It doesn’t matter. Next year, those raccoons / possums / super squirrels / lions / deer / garden gnomes or hobbits are in for a violent end. So violent that I will video it and post it to youtube and then watch it get taken down after children everywhere scream in horror after watching it.

I need a list of skills that you wish designers had

I’m team-teaching a course at Tri-C in the visual communications department this Fall. The class is for graphic designers, and we go through the experience of meeting with a client, building a prototype web site, revising it, then releasing it on the world, then going back and fixing any post-release issues.

The students all have excellent graphic design skills, but nothing in previous courses covers anything programming-related. The other instructor has the graphic design chops, and that’s really the meat of the course, but they do want to punch up the level of instruction beyond building static HTML into building simple web apps.

I need a short list of skills you wish all graphic designers have. Here’s what I plan to cover so far:

  1. How to use subversion. Probably we’ll use google code.
  2. How to use tools like Fiddler and Firebug to look at page weight and watch the requests go back and forth.
  3. How to check for valid HTML.
  4. How to do some simple AJAX (by hand at first and then later with jQuery).
  5. What’s the terminal in OS X for?
  6. HTTP gets versus posts and what the heck does caching mean?
  7. PHP fundamentals.
  8. I think I’m going to use PHP rather than anything else, but I’d like to hear arguments against that.

    Also, the class lab has a bunch of expensive Mac machines, so if there are really good tools out there, I’d love to hear about them.

Poultry, productivity, efficiency

I read a neat article in the last issue of Backyard Poultry. Modern breeds of chicken are really good at eating lots and lots of food so that they can grow really big as quickly as possible. So, now that prices for feed have gone way, way up, the costs of raising these birds have gone up just the same.

Meanwhile, there are other breeds of chickens that aren’t nearly as good at translating high-calorie feed into protein, but these birds need a lot less feed. They’re tougher animals — given enough pasture, they can survive almost entirely on scratching up bugs. Since feed has been cheap for so long, everybody lost interest in these breeds. After all, a skinny bird that just barely got by eating grubs and potato peels isn’t going to taste nearly as good as one that got fat really quickly off corn kernels.

Olympic mishap

Three comments:

  1. Why did they have a team of people ready to run out with visual shields to block the stage? Are horrifying injuries so expected that they plan how to hide them quickly?
  2. Who decided to play the slow-mo replay, followed by the super slo-mo replay?
  3. I’m using some hebrew font for the numbers in this list. Isn’t that neat?
Posted in mlp

This other Matt Wilson is missing.

He’s been gone since December. I’ve been following this since I saw people searching for “Matt Wilson” visiting my site. I don’t know if he ran away, or got abducted, or what else. It sounds like a nightmare for his family.

I hope the kid is OK. There’s a message board on that site and some guy asserts that Matt is living on the streets in Berkeley.

How to style an HTML link like a button?

I have some HTML links and I want to display them as buttons. I spent a few hours appying CSS styles, but never got something that appears identical to a button.

So this is what I’m doing now. It is obvious to read, and seems to work.


The W3 validator approved it in HTML 4 strict. So, all signs suggest that this is the way to go, but I don’t follow the semantic web conversation very closely.

So, for those of you that do pay attention to all that stuff, what’s bad about this approach?

Wilson Farm report for August, 2008

This is the first year I planted any flowers. I bought a sack of gladiolus bulbs from the big box hardware store. They’ve been really fun to watch.

Gladiolus

I never dug up a few of the onions from last year, and they survived the winter. Now they are making seeds at the top of each shoot. Onions and carrots take a few years to be able to reproduce.

I have a lot of respect for these onions now. They’re able to repel the hordes of rabbits in my backyard, they survived a freezing winter, and now they’re getting ready to spawn more. It’s a good thing humans and onions are allies.

Corn is doing well too, despite a late start and seed that is two years old. Last year, some critter in a single day ate all twenty stalks down to the ground. I think it was a deer that somehow found its way into my back yard. I’ve seen deer on Fairmount blvd in the morning, so it’s not impossible.



I spotted this cicada relaxing on one of my tomato stalks. He’s in the center of the picture, on the left side of the wooden stick. This photo doesn’t show his colorations well. His wings are iridescent like a dragonfly’s wings, and his shell is really shiny turquoise.

When Charlie was just learning to speak, he and I talked about what sounds we would hear. When we heard cicadas in the trees, he would say “train”. I guess they reminded him of trains. Now we call cicadas train bugs.

What else — this year, I’m growing brandywine tomatoes (some heirloom variety) and I’ve got plenty of of fat green ones. Brandywine tomatoes have pleats sort of like pumpkins.

In between rows of corn, I planted pumpkins and beans. Rabbits destroyed all the beans, but they ignored the pumpkins, and the vines now all have big orange flowers. I found two bees inside one flower.

Finally, I’ve been letting our 14-year old cat out into the backyard a lot to sun herself. The rabbits stay away when she’s around.

So, yeah, gardening is pretty fun.

The Dark Knight deconstructed

This story articulates my inchoate thoughts when I walked out of the theater after seeing The Dark Knight.

This blockquote will hook you or repel you:

Wayne uses his position as an Anglo-Saxon capitalist to marshal vast resources to develop military grade technology and materiel in the ‘fight’ against the ‘gangs’ in Gotham. Missiles, grenades, various projectiles, military or special-forces transportation methods (Fox borrows ideas from the CIA’s assassination squads quite overtly, i.e. “Skyhook”), mass surveillance, and old-fashioned brutality are Wayne’s stock-in-trade. All the while, he obfuscates his identity as the “Batman” in order to protect his position as capitalist, and to avoid public responsibility for his extra-legal violence. Of course, the actual Gotham police have no intention of arresting Batman for vigilante savagery, despite public acrimony over the rule of law.

I’ve written stuff in this tone, but this fellow is a master.

Posted in mlp