descriptors
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.