首页 > 代码库 > eval

eval

def eval(*args, **kwargs): # real signature unknown    """    Evaluate the given source in the context of globals and locals.        The source may be a string representing a Python expression    or a code object as returned by compile().    The globals must be a dictionary and locals can be any mapping,    defaulting to the current globals and locals.    If only globals is given, locals defaults to it.    """

用法一:将字符串中的数学表达式当成有效的表达式来求值并返回计算结果。

用法二:将字符串中的数据类型提取出来并返回此数据类型的数据 如将 ‘‘[1, 2, 2]‘‘转换成[1, 2, 3] 

print(eval(‘1+2+3‘))print(type(eval(‘[1, 2, 3]‘)))# 结果 6# 结果 <class ‘list‘>

  

 

eval