
Show HN: Compdescriptors: Python library for using composition - luther
https://github.com/luther9/compdescriptors-py
======
luther
The main tool in this module is the class 'Delegate'. It is a descriptor you
can use to forward a method or attribute name to an instance attribute. It can
be used like this:

    
    
        from compdescriptors import Delegate
    
        class Thing:
    
            def __init__(self):
                self.var = 'hello'
    
            def __len__(self):
                return 42
    
        class C:
            var = Delegate('thing')
            __len__ = Delegate('thing')
    
            def __init__(self):
                self.thing = Thing()
    

'compdescriptors' also contains tools like an abstract attribute descriptor
and a way to define interfaces. (These are not strictly necessary if you rely
on duck-typing.)

I'm wondering if I should rewrite the interface implementation. Currently,
Interface is a class whose instances can be used as class decorators. I could
make interfaces into classes that concrete classes can inherit from. This
would be a bit more natural than decorators, but I'm not sure how I would
implement the 'validate' method. (Maybe with a metaclass? But then all
concrete classes would get that method....) The advantage of using a decorator
is that I can modify a class in a very precise way without it inheriting
everything the interface object has.

