當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python InteractiveConsole runcode()用法及代碼示例

借助InteractiveConsole.runcode()方法,我們可以獲取單行或多行代碼的輸出,如果我們想查看部分代碼的結果,則可以使用InteractiveConsole.runcode()方法。

用法: InteractiveConsole.runcode(code)

返回:返回代碼部分的輸出。



範例1:
在這個例子中,我們可以通過使用InteractiveConsole.runcode()方法,我們可以使用此方法來獲取代碼的某些部分的結果,而無需運行整個代碼。

# import code 
from code import InteractiveConsole 
  
code = 'print("GeeksForGeeks")'
# Using InteractiveConsole.runcode() method 
InteractiveConsole().runcode(code)

輸出:

GeeksForGeeks

範例2:

# import code 
from code import InteractiveConsole 
  
code = 'a = 10; print(a + 20)'
# Using InteractiveConsole.runcode() method 
InteractiveConsole().runcode(code)

輸出:

30




相關用法


注:本文由純淨天空篩選整理自Jitender_1998大神的英文原創作品 InteractiveConsole runcode() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。