所以在寫一些模塊的程序 內使用,但使用i中的模塊的前內定義變量時遇到的困難:如何訪問在模塊內部定義的變量?
a.py
def func():
#do something to set the variable var
var = randomValue
b.py
from a import func
func()
#how do i get var
我將var設置爲全局,但仍未定義var。
所有這些答案看起來不錯,但沒有任何主題爲我的腳本工作。因此,這裏的模塊,也許你能告訴我怎麼去time_converted
到另一個腳本:
from ib.opt import Connection, message
import time
import datetime
list_time = []
global time_converted
def reply_handler(msg):
if msg.typeName == "currentTime":
time = msg.time
list_time.append(time)
time_converted = (datetime.datetime.fromtimestamp(int("%s"% time
)).strftime('%Y-%m-%d %H:%M:%S'))
return time_converted
def GetTime():
conn = Connection.create(port=7496, clientId=100)
conn.registerAll(reply_handler)
conn.connect()
conn.reqCurrentTime()
while True:
if len(list_time) == 0:
pass
elif len(list_time) == 1:
break
conn.disconnect()
除非你定義''的外FUNC var'()'或返回它,你不能 –
「所有這些答案看起來不錯,但沒有一個主題適用於我的腳本「究竟發生了什麼問題?你有錯誤信息嗎?你有錯誤的價值嗎?請具體說明。 – Xukrao
我只是得到「none」而不是任何值 – TB1