@maorongrong
2016-10-31T02:50:11.000000Z
字数 2984
阅读 892
Python
Date: 2016-10-31
在程序中经常会遇到需要将变量名先当作string存储,后续又需要用过string调出该变量名对应的变量。
有三种方法:
>>> var = "This is a string"
>>> varName = 'var'
>>> s= locals()[varName]
>>> s
'This is a string'
>>> s2=vars()[varName]
>>> s2
'This is a string'
>>> s3=eval(varName)
>>> s3
'This is a string'
locals()
Update and return a dictionary representing the current local symbol table. Free variables are returned by locals() when it is called in function blocks, but not in class blocks.Note
The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter.
locals是Python的内置函数,他可以以字典的方式去访问局部和全局变量,python里面用名字空间记录着变量.
每个模块,每个函数都有自己的名字空间,记录着变量,常量,类的命名和值,当python在使用变量时,会按照下面的步骤去搜索:
以上三个步骤,其中一步骤找到对应的变量,就不会再往下找。如果在这三个步骤都找不到,就会抛出异常。
locals与globals的区别
locals()是只读的。globals()不是。这里说的只读,是值对于原有变量的只读。其实还可以对locals()赋值的。
globals返回的是当前模块的全局变量 locals返回的是局部变量。注意,locals返回的是当前所在最小命名空间的局部变量的一个拷贝。
vars([object])
Return the dict attribute for a module, class, instance, or any other object with a dict attribute.Objects such as modules and instances have an updateable dict attribute; however, other objects may have write restrictions on their dict attributes (for example, new-style classes use a dictproxy to prevent direct dictionary updates).
Without an argument, vars() acts like locals(). Note, the locals dictionary is only useful for reads since updates to the locals dictionary are ignored.
本函数是实现返回对象object的属性和属性值的字典对象。如果默认不输入参数,就打印当前调用位置的属性和属性值,相当于locals()的功能。如果有参数输入,就只打印这个参数相应的属性和属性值。
eval(expression[, globals[, locals]])
The arguments are a Unicode or Latin-1 encoded string and optional globals and locals. If provided, globals must be a dictionary. If
provided, locals can be any mapping object.
Changed in version 2.4: formerly locals was required to be a dictionary. The expression argument is parsed and evaluated as a Python expression (technically speaking, a condition list) using the globals
and locals dictionaries as global and local namespace. If the globals
dictionary is present and lacks ‘builtins’, the current globals
are copied into globals before expression is parsed. This means that
expression normally has full access to the standard builtin module
and restricted environments are propagated. If the locals dictionary
is omitted it defaults to the globals dictionary. If both dictionaries
are omitted, the expression is executed in the environment where
eval() is called. The return value is the result of the evaluated
expression. Syntax errors are reported as exceptions. Example:>>> x = 1 >>> print eval('x+1') 2 This function can also be used to execute arbitrary code objects (such as those created by compile()). In this case pass a code object
instead of a string. If the code object has been compiled with 'exec'
as the mode argument, eval()‘s return value will be None.Hints: dynamic execution of statements is supported by the exec statement. Execution of statements from a file is supported by the
execfile() function. The globals() and locals() functions returns the
current global and local dictionary, respectively, which may be useful
to pass around for use by eval() or execfile().See ast.literal_eval() for a function that can safely evaluate strings with expressions containing only literals.
eval()函数十分强大,官方demo解释为:将字符串str当成有效的表达式来求值并返回计算结果。
结合math当成一个计算器很好用。
其他用法,可以把list,tuple,dict和string相互转化