我想建立一個可變數量的參數記錄器如下。我想這樣稱呼它:如何在Python打印內打印列表?
log(1, "Index: ", request_index, ", ", section)
def log(level, *msg) :
global print_debug_lastTime
currentTime = datetime.datetime.now().microsecond
# You can access global variables without the global keyword.
if g_debug_level & level != 0:
print("[DEBUG] " \
+ "%02d" % datetime.datetime.now().hour + ":" \
+ "%02d" % datetime.datetime.now().minute + ":" \
+ "%02d" % datetime.datetime.now().second + ":" \
+ str(currentTime) \
+ "%7d " % (currentTime - print_debug_lastTime) \
+ for m in msg)
但我無法打印可變數量的參數。起初我想在解釋器中運行這個簡單的代碼:
>>> print(str(x) for x in (0,1,2,3))
<generator object <genexpr> at 0x6ffffe5e550>
我想它打印0123
,但它打印出來作爲可能看起來。
那是因爲你有什麼打印裏面是一個生成器表達式,所以你可以這樣做:'print(list(str(x)for x in(0)) ,1,2,3)))'。 – idjaw
謝謝! 'print(「」.join([str(x)for(in(0,1,2,3)]))''做到了。 – user