简体   繁体   English

如何在Python中将字符串作为函数调用

[英]How to call a string as a function in Python

I have a string A: 我有一个字符串A:

import keyboard

A = "keyboard.press_and_release('left windows+R')"

But, I want it to work as a function. 但是,我希望它可以作为一个功能。 If I call A, then it should press Windows Key and R key present on y keyboard. 如果我呼叫A,则应按y键盘上的Windows键和R键。

You can use eval : 您可以使用eval

a = "keyboard.press_and_release('left windows+R')"
eval(a)

Try using eval . 尝试使用eval See https://docs.python.org/3.6/library/functions.html#eval or What does Python's eval() do? 请参阅https://docs.python.org/3.6/library/functions.html#evalPython的eval()做什么?

import keyboard

A = "keyboard.press_and_release('left windows+R')"
eval(A)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM