class MyModel(object) : pass
modelClass = MyModel()
class ModelResource(object):
def mymethod(self):
print('got here')
Meta = type('Meta', (object, ), {'allowed_methods': ['get']})
def add_mymethod(cls):
def mymethod(self):
super(cls, self).mymethod()
cls.mymethod = mymethod
return cls
name = modelClass.__class__.__name__ + "Resource"
MyModelResource = add_mymethod(type(name, (ModelResource, ),
{'Meta':Meta, }))
print(MyModelResource.Meta)
# <class '__main__.Meta'>
m = MyModelResource()
m.mymethod()
# got here
The inner class, Meta, is just another attribute as far as MyModelResource is concerned.
Methods are also just attributes as far as MyModelResource is concerned. Actually, you define a function in MyModelResource.__dict__, and the Python attribute lookup mechanism
causes inst.mymethod to return a bound method.
There is no problem referring to MyModelResource in the super call
super(MyModelResource, self).mymethod()
before MyModelResource is defined, because name lookups are performed at run time, not at the time mymethod is defined.
You are absolutely correct that
super(self.__class_, self).mymethod()
is wrong. This will spoil all that is good about super. If MyModelResource were to be subclassed, and an instance of the subclass were to call mymethod, then Python would fall into an infinite loop.