I have a programming idea: Instead of making null (abstract) base classes, make identities.
So, don't do this: class Animal:def talk(self):
pass
def run(self, over_there):
pass Rather, do this:
class Animal:
def __init__(self, animal):
self.animal = animal
def talk(self):
self.animal.talk()
def run(self, over_there):
self.animal.run(over_there) Why?Well, I figure, as long as you're just putting "pass" or "= 0" in all those functions, why not put something useful?And then: You get decorators for free.The same goes for interfaces -- as long as they're "just sitting there," why not make the default behavior pass-through?
class IFoo:
def __init__(self, target):
self.target = target
def foo(self, xyzzy):
self.target.foo(xyzzy)
def bar(self, xyzzy):
self.target.bar(xyzzy) That way, if you want to make a decorator that just does something for foo, you inherit the interface, and just override foo.This may seem trivial here, but I work with a lot of classes that have something like 20-30 members and data items. Often, these complex classes are *great* candidates for decorating, but often, I don't do it because I just don't want some 50 lines of inane "foo = foo" code, and all the hassle should I decide to add a function. Work-around: Just build it into the interface.