class blah(AutoProp):
foo = ReadOnlyProperty()
bar = IntProperty()
baz = FloatProperty()
Cool. But I wanted more power for my properties than just converting to a number. So I started thinking about a metaclass to transform something like this:
class Craziness(CrazyAutoProp):
def transform_foo(self, value):
# Insert all kinds of craziness here
into something like:
class FooDescriptor(object):
def __get__(self, instance, klass):
return instance._foo
def __set__(self, instance, value):
value = self.transform(instance, value)
instance._foo = value
def transform(self, value):
# Insert all kinds of craziness here
transform = staticmethod(transform)
class Craziness(object):
foo = FooDescriptor()
That was the point where the benefits seemed to move from writing legible code to writing less code. And also when I started wondering if the first example is even legible. Or if any of the metaclass code I've written for work in the past month is going to be comprehensible to anyone other than me. Bad Ryan! No more metaclasses for you.
(Note: As soon as I posted this I started reading the "What's New in Python 2.4" to see if I could learn about any other dark magic. Oh boy. At least I won't be able to use any of that at work)
No comments:
Post a Comment