In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the expression of the eval function in Python, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
What does eval do?
Parses the string expression and executes it, and returns a value
Syntax format eval (expression [, globals [, locals]])
Expression: expression string
Globals: must be a dictionary
Locals: can be any map object
The simplest expression, Chestnut.
Chestnut one
Print (eval ("123") print (eval (" True ")) print (eval (" ((1meme 2jue 3) ")) print (eval (" [1meme 2mei 3])) # output result 123True (1meme 2jue 3) [1meme 2jue 3]
Chestnut two
Print (eval ("1x 2")) x = 1print (eval ('x # 1')) # output result 32
Chestnut three
A = 1b = 2print (eval ("[AMaginb]")) # output result [1,2] with globals# use globalsx = 10g = {"x": 5} print (eval ("x" 1 ", g)) # output result 6
The globals parameter is provided in eval
The scope of eval is the dictionary specified by g, the outer x = 10 is masked, and eval is invisible, so a value of x = 5 is used.
X = 10y = 5g = {"x": 5} print (eval ("x 1y", g)) # output result 5 print (eval ("x x 1y", g)) File ", line 1, in NameError: name 'y' is not defined
Because the global parameter has no y variable value, it has been reported incorrectly
With locals#, use localsa = 1g = {"a": 2, "b": 3} l = {"b": 30, "c": 4} print (eval ("a+b+c", g, l)) # output result 36
The scope of eval has become globals + locals
Locals scope priority will take precedence over globals
The value in the locals parameter overrides the value in the globals parameter
String-to-name':'linux','age':age dictionary # string-to-string dictionary jsons = "{'axiaqiaohuo 123" print (type (eval (jsons) # output with globalsprint (eval ("{'name':'linux','age':age}", {"age": 123}) # output {' name':'linux','age': 123} with localsprint (eval ("{'name':'linux','age':age}") {"age": 123}, {"age": 24}) # output result {'name':' linux', 'age': 24} built-in function Chestnut # built-in function print (eval ("dir ()") print (eval ("abs (- 10)") # output result [_ _ annotations__',' _ builtins__','_ cached__','_ doc__','_ file__' '_ _ loader__',' _ _ name__','_ _ package__','_ _ spec__','a chestnut, 'baked,' grubbed, 'jsons',' lager, 'xmom,' y'] 10 false chestnuts
Chestnut one
Print (eval ("aa")) # output result print (eval ("aa")) File ", line 1, in NameError: name 'aa' is not defined
Chestnut two
Print (eval ("[a ~ ~ b ~ m ~ c])) # output result print (eval (" [a ~ ~ b ~ c] ")) File", line 1, in NameError: name'c'is not defined
Chestnut three
Print (eval ("if x: print (x)") # output result print (eval ("if x: print (x)") File ", line 1 if x: print (x) ^ SyntaxError: invalid syntax
Because eval () only accepts expressions any other statements (such as if, for, while, import, def, class) will throw an error
Thank you for reading this article carefully. I hope the article "what are the expressions of eval functions in Python" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.