I want to embellish a class from a python module. How do I do this?
Question: I am using a class from a module that I import. I want to extend this class:
add some more attributes/methods. However, I want to retain all the
functionality, including all possible constructors, of the original class.

Here is one way I can do it:

    
    
    class Extension(module.ModuleClass):
        def __init__(self, *args, **kwargs):
            newargument = kwargs.pop("myarg")
            super(Extension, self).__init__(*args, **kwargs)
            self.newargument = newargument
    

This seems to work, but has a minor syntactic sugar issue and the following
much bigger isssue. ModuleClass overloads the "`+`" etc. operators. Of course,
Extension now benefits from this overloading, but as a result of how these are
implemented in ModuleClass, the result is of type ModuleClass, not Extension.
That is, for `Extension` objects `a` and `b`, the result `a + b` is well
defined, but has type `ModuleClass`, not `Extension`. This is crucially
important..

Is there a better way to achieve extension such as what I am looking for?
Better in the senses of type-preservation as a result of operations, easier to
debug down the line, easier to maintain, and more efficient?

Specifically, is there a way to use composition rather than inheritance? I.e.,
to write a wrapper class that **has as attribute** a `ModuleClass`, is able to
redirect all attributes (including methods) to the contained `ModuleClass`,
but handles functionality related to new arguments etc. itself?

The following is my failed attempt at using composition to solve the problem:

    
    
    class Extension(object):
        def __init__(self, *args, **kwargs):
            self.myarg = kwargs.pop('timestamp')
            self.mclass = module.ModuleClass(*args, **kwargs)
        def __getattribute__(self, name):
            if (name == 'mclass'):
                return self.mclass
            elif (name == 'myarg'):
                return self.myarg
            else:
                return getattr(self.mclass, name)
    

But this fails to achieve what I want to (make `Extension` look like `Module`
with extensions). What care is to be taken?

**Minor** syntactic sugar issue: the constructor arguments now have to include
"myarg = something", which is not the most desirable. (I would have preferred
if i could have the constructor take the first argument, OR an argument
specified with "myarg = something" to initialize self.newargument. However,
this is a minor issue. Ideally, I would like to support constructor calls of
the form

    
    
    Extension(validValueForMyarg, validStuffToInitModuleClass)
    Extension(validStuffToInitModuleClass, myarg = validValueForMyarg)
    

and, where it makes sense,

    
    
    Extension(myarg = validValueForMyarg, validStuffToInitModuleClass)
    


Answer: You can add a required argument to `Extension.__init__` like so:

    
    
    class Extension(module.ModuleClass):
        def __init__(self, myarg, *args, **kwargs):
            super(Extension, self).__init__(*args, **kwargs)
            self.newargument = myarg
    

