Archive for October, 2008

Fun with Descriptors in Python

I always like discovering new features in my favorite languages, especially when it scratches an itch I’ve been having. A week ago, I was wishing I could have a method in Python be both an instance method and a class method, though now I can’t recall exactly why. But last night, I saw Ian Bicking’s article Decorators and Descriptors, and I realized I could do that with descriptors.

Descriptors, for those of you who didn’t read the link above, are functions that are run when methods (or attributes) are looked up on new-style classes. The default descriptor on functions is what’s responsible for sticking the self at the front of the argument list. Here’s one that changes the function based on whether it’s being called on a class or an instance:

Still not sure what this would be useful for, but I thought I’d paste it here, because it was fun.

Tags: ,

Friday, October 24th, 2008 Write View Comments

Isshoukenmei

I’ve been away from blogging for quite a while, and I’ve suffered for it. Every day I let go by without engaging some new programming tool or skill is a day I grow weaker. So I’m making a commitment to write every day about something I’ve done to engage. Unfortunately, now it’s 2:35 AM and I really should sleep, so this one’s really short.

Steve Yegge wrote an excellent essay on prototype-based object mapping. It’s of interest to me because my company’s app has had two different half-assed (and differently half-assed) implementations of the prototype pattern, one of which we’ve just removed. We’re using it to model people (well, actually, contact information), but people don’t have just single-valued properties. They have names with multiple parts, and multiple phone numbers (how many people these days have not just a cell phone but a work cell and a personal cell?) and many other complex values, and I’ve been at a loss for a good way to store these. Steve’s essay has helped to convince me that we don’t need to discard the prototype pattern; we just need to get it right for this use.

In the next few days, I hope to blog here about getting it right.

Tags: ,

Wednesday, October 22nd, 2008 Write View Comments