I'm trying to implement this: How to fake type with Python. I'm wrapping some .NET objects I don't control, so don't want __new__
to be called on them by the instantiation of the class in the wrapper. I've taken the code from the question above and built a simple example:
class WrappableInt(object):
def __new__(cls,*args,**kwargs):
print("Making a new wrapint")
return super().__new__(cls)
def __init__(self,x):
self.x=x
def wrap_user(wrapee):
class wrapped_user(type(wrapee)):
def __new__(cls,*args,**kwargs):
return object.__new__(cls)
def __init__(self):
pass
def gimmieFive(self):
return 5
def __getattribute__(self, attr):
self_dict = object.__getattribute__(type(self), '__dict__')
if attr in self_dict:
return self_dict[attr]
return getattr(wrapee, attr)
return wrapped_user()
When I run the following tests, everything is as expected:
>>> z=WrappableInt(3)
Making a new wrapint
>>> p=wrap_user(z)
>>> print(p.x)
3
However, when I run gimmieFive, I get the following:
>>> p.gimmieFive()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: gimmieFive() missing 1 required positional argument: 'self'
Why does python think gimmieFive
is a static method, when it seems to know __getattribute__
is not? It will happily run e.g.:
>>> p.gimmieFive(1)
5