当前位置:网站首页>Introduction and usage of Eval function
Introduction and usage of Eval function
2022-06-11 10:50:00 【Trafalgar peak】
# eval function
# eval(expression,globals=None,locals=None)
# expression: This parameter is a string ,python Will use globals Dictionary and locals Dictionaries as global and local namespaces , take expression As a python
# expression ( Technically speaking , Is a list of conditions ) Analyze and calculate .
# globals: This parameter controls a global namespace , That is, we can use functions in the global namespace when calculating expressions , If this parameter is provided ,
# And there is no custom __builtins__, Then the current environment __builtins__ Copy to your own globals in , Then we can do the calculation .
# About __builtins__, It is python Built in modules for , That is to say python Built in modules , There's no need for us import It can be used , For example, we usually use int、
# str、abs And so on in this module .
# locals: This parameter controls a local namespace , and globals similar , But when it and globals When there is a repeating part in ,locals The definition in will override globals
# Medium , That is to say globals and locals When there are conflicting parts in ,locals The final say , It has the right to decide , Subject to its . If locals Has not been Words provided , The default is globals.
Here are some simple use cases :
# The string str Evaluate as a valid expression and return the evaluation
a = "3*2"
print(eval(a)) # Take off the outer layer "", Evaluate as an expression , Return at this time 6
a = 10
print(eval("a+1")) # return 11
# Slice fetching
a = "1234"
print(eval(a[0])) # Take the string No 0 Elements , Return at this time 1
print(eval(a[2])) # return 3
# stay globals When specified :
a = 10
g = {'a': 4}
print(eval("a + 1", g)) # The return value is 5, Because it provides globals Parameters , At this time eval The scope is g Specified Dictionary , That is to say, outside a=10 Shielded
# stay locals When specified :
a = 10
b = 20
c = 30
g = {'a': 6, 'b': 8}
t = {'b': 100, 'c': 10}
print(eval('a+b+c', g, t))
# The return value is 116,g Mask out global variables a and b,locals And globals There was a conflict , here locals Play a decisive role ,b The value is t Medium b value 100
a = '["a", "b", "c"]'
print(eval(a)) # return ['a', 'b', 'c']
边栏推荐
- [DBSCAN] DBSCAN instance
- Remote monitoring project offline log specification
- [K-means] K-means learning examples
- Pyramidtnt: TNT with characteristic pyramid structure
- Yibentong 1122: calculating saddle point
- Is it safe to open an account online? Can ordinary people drive it?
- [CV basis] Color: rgb/hsv/lab
- Half of the property rights of the house are registered in the woman's name when they are in love, and they want to return after they break up
- 金仓数据库KingbaseES UDP监控工具的使用
- js设置ip屏蔽
猜你喜欢

VMware install win7 virtual machine

微信云开发Al短视频一键换脸小程序源码

NFT 2.0: the next generation of NFT will be lean and trustworthy

Kingbasees create database objects in batch

NFT 2.0: 下一代的NFT将是精简且值得信赖的NFT

Fix the problem that uicollectionview does not reach the bottom security zone

金仓数据库KingbaseES中的sys_checksums坏块检测功能
![[machine learning theory] true positive, true negative, false positive, false negative concept](/img/59/8264d6cbd96480b59e5b8ff96320be.png)
[machine learning theory] true positive, true negative, false positive, false negative concept

Pl/sql compilation check in kingbasees

地铁路线图云开发小程序源码和配置教程
随机推荐
Cloud development MBTI personality type test assistant wechat applet source code
1712. 将数组分成三个子数组的方案数 ●●
Ngui, map zoom in and out
Série de démarrage C # (XI) - - tableaux multidimensionnels
Mxnet construction and implementation of alexnet model (comparison with lenet)
MySQL download, installation and use - complete and detailed steps
Ngui, select gender male and female
White screen time, first screen time
NFT 2.0: the next generation of NFT will be lean and trustworthy
金仓数据KingbaseES 批量创建数据库对象
5.读取指定路径名-dirname
杰理之BLEPR0 和 PR1 当普通 IO 口使用【篇】
距离度量 —— 欧式距离(Euclidean Distance)
Development and source code construction of digital collection system
Batch add noise to data and generate new named annotation files
golang编译和链接参数,运行时
金仓数据库KingbaseES UDP监控工具的使用
MXNet对AlexNet模型的构建与实现(与LeNet的对比)
International multilingual sea mall rebate product automatic matching order source code
Design and implementation of interest social software platform based on location service (LBS) SSM framework