[英]Python: class methods calling other class methods when initial method is called from within another class
我正在開發一個精心設計的文件切換器(缺乏更好的術語),但遇到了問題。 簡化,考慮下面的代碼:
class Foo(object):
def __init__(self):
self.foo = Bar()
self.foo.baz()
class Bar(object):
def baz(self):
print("baz")
self.qux()
def qux(self):
print("qux")
當類 Foo 啟動並調用“self.foo.baz()”時,輸出中不會打印任何內容。 怎么來的? 在這種情況下,有沒有辦法從 Foo 中正確調用 Bar 類中的方法?
__init__
在您創建實例之前不會被調用。 如果您創建一個實例:
a = Foo()
然后你應該看到打印的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.