0

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.

1 Answer 1

1

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>}
Sign up to request clarification or add additional context in comments.

1 Comment

That's interesting. I didn't think of that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.