PythonNote034---把字符串当做代码执行_python 把字符串当成代码执行-CSDN博客

网站介绍:文章浏览阅读189次。Intro有时候,需要把字符串当做代码执行,看个例子getattrGet a named attribute from an object; getattr(x, ‘y’) is equivalent to x.yimport pandas as pddf=pd.DataFrame({"x":[1,2,3]})getattr(df,"x").count()3getattr(df,“x”)相当于df.x如果想执行df.x*2怎么弄?evaleval("df.x*2")0 _python 把字符串当成代码执行