1

assume i have a class from library:

class A:
  def foo():
    pass
  def bar():
    pass

class B(A):
  def anotherFunction():
    pass

and i have a third library function that returns A, but i want to pass my code my enriched implementation, i mean class B. how can i take the A instance and insert it into B?

yes i thought about setattr for A instance but it won't help me with the typing completion. also i can pass in theh c'tor of B the A instance, and then implement all A functions in B just by calling the implementation of A instance. but its sound horrible, im sure im missing something, can you help?

thanks alot!

arielttt0
  • 59
  • 2
  • Rather than writing a subclass `B`, it sounds like you want to [monkey patch](https://stackoverflow.com/questions/5626193/what-is-monkey-patching) the class `A`. Instead of using `setattr` on each instance, just write `A.anotherFunction = ...` after declaring the function outside of the class. If that doesn't give you the type-hinting/autocomplete suggestions you want, then your question should be *"how do I get type-hinting/autocomplete to work with a monkey-patched class?"* – kaya3 Feb 20 '21 at 15:37

0 Answers0