我試圖在variable內部訪問function_3- 我應該如何去做?
class A:
def function_1(self):
def function_2(self):
self.variable = 'Hello'
function_2(self)
function_1(self)
def function_3(self):
print(self.variable)
function_3(self)
uj5u.com熱心網友回復:
self這里的名字def function_2(self):陰影self了這里的名字def function_1(self):
class A:
def function_1(self):
def function_2(self): # Here name `self` shadows name `self` from previous line
self.variable = 'Hello'
function_2(self)
function_1(self) # Here you have no `self` variable
def function_3(self):
print(self.variable)
function_3(self) # Here you have no `self` variable too
我認為你想實作這種行為
class A:
def function_1(self):
def function_2(x):
x.variable = 'Hello'
function_2(self)
def function_3(self):
print(self.variable)
a = A()
a.function_1()
a.function_3()
> Hello
uj5u.com熱心網友回復:
如果我誤解了您想要做什么,請大聲說出來,但看起來您需要使用 A() 創建類 A 的實體,然后在該實體上呼叫 function_1 和 function_3。原諒所有的術語,但我希望你能從下面的例子中學習:
class A:
def function_1(self):
def function_2(self):
self.variable = 'Hello'
function_2(self)
def function_3(self):
print(self.variable)
a = A()
a.function_1()
a.function_3()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/396332.html
上一篇:如何通過臨時命令將多個值傳遞給“with_items”?
下一篇:變數內的值的名稱是什么?
