I am using a class from a module that I am importing. I want to extend this class: add some more attributes / methods. However, I want to keep all the functionality, including all possible constructors, of the source class.
Here is one way to do this:
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 syntax sugar issue and the following much more serious problems. ModuleClass overloads " +" operators, etc. Of course, Extension now benefits from this overload, but as a result of how they are implemented in ModuleClass, the result is of type ModuleClass, not Extension. That is, for Extensionobjects a, the bresult is a + bcorrectly defined, but it has a type ModuleClass, and not Extension. This is crucial.
Is there a better way to achieve expansion, for example, what am I looking for? Better in terms of saving types as a result of operations, is it easier to debug a line, easier to maintain, and more efficient?
, , ? .. , a ModuleClass, ( ) ModuleClass, , ..?
:
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)
, ( Extension Module ). ?
: "myarg = something", . ( , , "myarg = something" self.newargument. .
Extension(validValueForMyarg, validStuffToInitModuleClass)
Extension(validStuffToInitModuleClass, myarg = validValueForMyarg)
, ,
Extension(myarg = validValueForMyarg, validStuffToInitModuleClass)