Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
79 views
in Technique[技术] by (71.8m points)

python - How to subclass within a complicated hierarchy?

If anyone can give me a better way to phrase my question in the title, I'll change it.

I'm using a library1 and from that library I import some high level class:

from library import HighLevelClass

In the library, HighLevelClass has an attribute which is an instance of another class, and that in turn has an attribute which is an instance of yet another class, and so on...

class Bar():
    self.foobar = FooBar()

class Foo():
    self.bar = Bar()

class HighLevelClass:
    self.foo = Foo()

Now, I want to be able to replace some method of Foo for instance. I can't just do:

class MyFoo(Foo):
    def foo_method(self):

because HighLevelClass will still be referencing the Foo in the library, not my Foo.

I could try to unravel the hierarchy and subclass the HighLevelClass and make my way down to Foo subclassing everything as I go, but that's just madness.

What's the correct way to do what I'm trying to achieve?

Footnotes

1 It doesn't really matter for the purposes of the question but here it is: facebookresearch/detectron2

question from:https://stackoverflow.com/questions/65939062/how-to-subclass-within-a-complicated-hierarchy

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...