Reputation: 123
I was hoping to be able to do this:
class C:
def __init__(self, foo:Callable):
self._foo = foo
class D:
bar:'C' = C(D.ping) # Unresolved reference 'D'
# I also tried this;
# bar:'C' = C(ping) # Unresolved reference 'ping'
def __init__(self):
...
def ping(self, pong:str):
...
I was surprised this didn't work. Is this not possible?
You can obviously do this:
...
class E:
bar:'C' = C(D.ping)
def __init__ ... #etc
This is a very similar question: Calling class staticmethod within the class body?
But perhaps different because they are trying to call a static method, while I am trying to pass it as a parameter.
Upvotes: 0
Views: 141
Reputation: 32233
As a workaround, you could put bar
definition after ping
definition:
class D:
print(locals())
def __init__(self):
...
def ping(self, pong: str):
...
print(locals())
bar: 'C' = C(ping)
{'__module__': '__main__', '__qualname__': 'D', '__annotations__': {}}
{'__module__': '__main__', '__qualname__': 'D', '__annotations__': {}, '__init__': <function D.__init__ at 0x7f18f5de0e50>, 'ping': <function D.ping at 0x7f18f5de0ee0>}
Upvotes: 1