码迷,mamicode.com
首页 > 其他好文 > 详细

eval

时间:2016-12-05 14:14:58      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:提取   unknown   val   字符   app   string   取出   nbsp   div   

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

标签:提取   unknown   val   字符   app   string   取出   nbsp   div   

原文地址:http://www.cnblogs.com/lcgsmile/p/6133538.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!