Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the function of Python Library?

Shulou Source: shulou.com Published: 2022-06-02 23:32:03 09月25日 Update

This article is to share with you what the function of Python Library is, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Running code strings directly is also an important feature of dynamic languages. Although similar functionality can be achieved through CodeDom in .NET / C #, it is not nearly as convenient and free as Python.

> code = "def test (s): print" test: ", s a = 123" > exec code > a 123 > test ("abc") test: abc

The built-in functions also include eval () and execfile (), which are used to do similar things. The exec keyword executes multiple lines of code snippets, and the eval () function is usually used to execute an expression that contains a return value, while execfile is naturally used to execute source files.

> a = 10 > x = eval ("a + 3") > x 13

Both eval () and execfile () have "globals, locals" parameters, which are used to pass environment variables, and globals () and locals () are used directly to get the data of the current scope when the default or explicit setting is None.

> x = eval ("a + b", {}, {})

Pass a null value so that it cannot get local information

Traceback (most recent call last): File ", line 1, in x = eval (" a + b ", {}, {}) File", line 1, in NameError: name'a'is not defined > x = eval (" a + b ", {}, {" a ": 123," b ": 2})

Explicit transmission of environmental information

> I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Function code information function more environment knowledge article practical important free thing function key keyword dynamic parameter variable character string Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Technology OPPO Reno Xiaomi Shulou Information