Having the following code, what can I do with the ‘foo’ function to make it unbound in any case even if it is assigned to class attribute? Overriding __get__ doesn’t help – as far as I understand because it is not used when function is absent from __dict__ of an instance (it is so in case of class attributes).
But what else can be done here?
def foo(x):
print(x)
def foo_get(self, obj, type=None):
return foo
foo.__get__ = foo_get
class A(object):
def __init__(self):
self.f = foo
class B(object):
f = foo
a = A()
print(a.f) #<function foo at 0x2321d10>
print(a.f.__get__(a, A)) #<function foo at 0x2321d10>
b = B()
print(b.f) #<bound method B.foo of <__main__.B object at 0x23224d0>>
I’m pretty sure you want @staticmethod.
foo = staticmethod(foo)You can also define
__get__on a callable class:It’s one extra level of indentation and a few extra lines of code, but that’s how it can be done, if it’s important.