我正在嘗試定義一個函式,但我不能呼叫它:
def test():
fig = make_subplots(rows=1, cols=2, specs=[
[{'type': 'scatter'}, {'type': 'table'}]]
, shared_xaxes=True,
horizontal_spacing=0.05,
vertical_spacing=0.05,
column_widths=[0.7, 0.3])
pass
test()
fig.add_trace(go.Candlestick(x=df.index,
open=df['Open'],
high=df['High'],
low=df['Low'],
close=df['Close'],
increasing_line_color = UP_CANDLE,
decreasing_line_color = DW_CANDLE),row=1,col=1)
當我呼叫它時,我檢索到以下錯誤: NameError: name 'fig' is not defined 如果我洗掉該函式,代碼將完美運行。
uj5u.com熱心網友回復:
一旦你的區域變數fig在函式內部,除非你從函式中回傳 fig ,否則函式外部的任何東西test都無法訪問 fig ——你可以在 python中閱讀有關范圍的資訊。
我會像這樣重寫你的函式(并且你的函式pass沒有任何目的,所以我洗掉了它):
def test():
fig = make_subplots(
rows=1, cols=2,
specs=[[{'type': 'scatter'}, {'type': 'table'}]],
shared_xaxes=True,
horizontal_spacing=0.05,
vertical_spacing=0.05,
column_widths=[0.7, 0.3]
)
return fig
然后你可以設定一個等于你的函式回傳的變數:
fig = test()
然后你就可以訪問.add_trace`fig
fig.add_trace(go.Candlestick(x=df.index,
open=df['Open'],
high=df['High'],
low=df['Low'],
close=df['Close'],
increasing_line_color = UP_CANDLE,
decreasing_line_color = DW_CANDLE),row=1,col=1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/450539.html
上一篇:在C 中列印數字的階乘程式
