Is there a way to simulate the usage of self parameter on an object created using type() method?
I want to be able to access the object which was used for calling the method. If I had defined a class then I will be able to access to that object using self parameter. But is it possible to do the same with object created using type()?
In the example below I want to access the instance on which the method getname() was called. (I don't want to pass the object as argument.)
def getname(self):
return getattr(self, 'name')
obj = type('student', (object,), {"getname":getname})
obj2 = type('student', (object,), {"getname":getname})
setattr(obj, 'name', 'john')
setattr(obj2, 'name', 'peter')
print(obj.getname())
print(obj2.getname())