1. <source id="nejs2"></source>
  2. <video id="nejs2"></video>
    <source id="nejs2"></source>
          1. Python eval()

            更新:

            eval()函數執行作為參數給出的表達式。表達式被計算并解析為有效的 python 語句。表達式始終是字符串,并且對于eval()函數是必需的。

             **eval(expression, globals=None, locals=None)** #Where expression can be a string to evalate 
            

            評估()參數:

            取 3 個參數,其中第一個參數是強制的,另外兩個是可選的。

            參數 描述 必需/可選
            表情 這個參數是一個字符串,它被解析并作為 python 表達式執行 需要
            全球 這是一本字典。它用于指定可執行的表達式。 可選擇的
            本地人 它指定eval()中的局部變量和方法 可選擇的

            評估()返回值

            | 投入 | 返回值 | | 表示 | 表達式的結果 | | 省略了全局變量和局部變量 | 表達式在當前范圍內執行 | | 全局存在;本地人被省略了 | 全局變量和局部變量都將使用全局變量 | | 全球和本地都存在 | 根據參數的結果 |

            Python 中eval()方法的示例

            示例 1:演示eval()的使用

             # Perimeter of Square
            def calculatePerimeter(l):
                return 4 * l
            
            # Area of Square
            def calculateArea(l):
                return l * l
            
            exp = input("Type a function: ")
            
            for l in range(1, 5):
                if exp == "calculatePerimeter(l)":
                    print("If length is ", l, ", Perimeter = ", eval(exp))
                elif exp == "calculateArea(l)":
                    print("If length is ", l, ", Area = ", eval(exp))
                else:
                    print("Wrong Function")
                    break 
            

            輸出:

            Type a function: calculateArea(l)
            If length is  1 , Area =  1
            If length is  2 , Area =  4
            If length is  3 , Area =  9
            If length is  4 , Area =  16 

            示例 2:當全局參數和局部參數都被省略時,eval()的工作方式。

             from math import *
            print(eval('dir()')) 
            

            輸出:

            ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'os', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc'] 

            示例 3:將空字典作為全局參數傳遞

             from math import *
            print(eval('dir()', {}))
            
            # The code will raise an exception
            print(eval('sqrt(25)', {})) 
            

            輸出:

            ['__builtins__']
            Traceback (most recent call last):
              File "<string>", line 5, in <module>print(eval('sqrt(25)', {}))
              File "<string>", line 1, in <module>NameError: name 'sqrt' is not defined</module></string></module></string> 

            示例 4:使某些方法可用

             from math import *
            print(eval('dir()', {'sqrt': sqrt, 'pow': pow})) 
            

            輸出:

            ['__builtins__', 'pow', 'sqrt'] 

            示例 5:傳遞全局和局部字典

             from math import *
            
            a = 169
            print(eval('sqrt(a)', {'__builtins__': None}, {'a': a, 'sqrt': sqrt})) 
            

            輸出:

            13.0 
            頂部 久久久久99精品成人片毛片_黃色A片三級三級三級无码_日本不卡高清视频v中文字幕_高清欧美视频一区二区
            1. <source id="nejs2"></source>
            2. <video id="nejs2"></video>
              <source id="nejs2"></source>