本文介绍了在Python中,你可以调用A类的实例方法,但是传递B类的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 为了重用一些被定义为不同类的实例方法的现有代码,我正在做类似下面的事情: class Foo(object): def __init __(self): self.name =Foo def hello $ b printHello,I am+ self.name +。 class Bar(object): def __init __(self): self.name =Bar bar = () Foo.hello(bar) 但结果是:Is something like this possible?I should have been clear that I know this is a bad idea. Obviously the real solution is a bit of refactoring. I just figured there must be a way, and it turns out there is.Thanks for the comments. 解决方案 Looks like this works:Foo.hello.im_func(bar)I guess I need to read a this little harder... 这篇关于在Python中,你可以调用A类的实例方法,但是传递B类的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-15 21:06