您可以使用eval()函数从字符串执行代码
一个例子是:
import math
test=r"dir(math)"
eval(test)
Run Code Online (Sandbox Code Playgroud)
产量
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'hypot', 'isinf', 'isnan', 'ldexp', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
Run Code Online (Sandbox Code Playgroud)
但是使用eval是非常不安全的,我建议你安全地使用eval()
| 归档时间: |
|
| 查看次数: |
2396 次 |
| 最近记录: |