Python內置函數(12)——compile

来源:http://www.cnblogs.com/sesshoumaru/archive/2016/10/22/5986751.html
-Advertisement-
Play Games

英文文檔: compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) ...


英文文檔:

compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)

Compile the source into a code or AST object. Code objects can be executed by exec() or eval(). source can either be a normal string, a byte string, or an AST object. Refer to the ast module documentation for information on how to work with AST objects.
The filename argument should give the file from which the code was read; pass some recognizable value if it wasn’t read from a file ('<string>' is commonly used).
The mode argument specifies what kind of code must be compiled; it can be 'exec' if source consists of a sequence of statements, 'eval' if it consists of a single expression, or 'single' if it consists of a single interactive statement (in the latter case, expression statements that evaluate to something other than None will be printed).
The optional arguments flags and dont_inherit control which future statements (see PEP 236) affect the compilation of source. If neither is present (or both are zero) the code is compiled with those future statements that are in effect in the code that is calling compile(). If the flags argument is given and dont_inherit is not (or is zero) then the future statements specified by the flags argument are used in addition to those that would be used anyway. If dont_inherit is a non-zero integer then the flags argument is it – the future statements in effect around the call to compile are ignored.
Future statements are specified by bits which can be bitwise ORed together to specify multiple statements. The bitfield required to specify a given feature can be found as the compiler_flag attribute on the _Feature instance in the __future__ module.
The argument optimize specifies the optimization level of the compiler; the default value of -1 selects the optimization level of the interpreter as given by -O options. Explicit levels are 0 (no optimization; __debug__ is true), 1 (asserts are removed, __debug__ is false) or 2 (docstrings are removed too).
This function raises SyntaxError if the compiled source is invalid, and ValueError if the source contains null bytes.
If you want to parse Python code into its AST representation, see ast.parse().
Note: When compiling a string with multi-line code in 'single' or 'eval' mode, input must be terminated by at least one newline character. This is to facilitate detection of incomplete and complete statements in the code module.
Changed in version 3.2: Allowed use of Windows and Mac newlines. Also input in 'exec' mode does not have to end in a newline anymore. Added the optimize parameter.
Changed in version 3.5: Previously, TypeError was raised when null bytes were encountered in source.
說明:
  1. 將source編譯為代碼或者AST對象。代碼對象能夠通過exec語句來執行或者eval()進行求值。
  2. 參數source:字元串或者AST(Abstract Syntax Trees)對象。即需要動態執行的代碼段。
  3. 參數 filename:代碼文件名稱,如果不是從文件讀取代碼則傳遞一些可辨認的值。當傳入了source參數時,filename參數傳入空字元即可。
  4. 參數model:指定編譯代碼的種類,可以指定為 ‘exec’,’eval’,’single’。當source中包含流程語句時,model應指定為‘exec’;當source中只包含一個簡單的求值表達式,model應指定為‘eval’;當source中包含了互動式命令語句,model應指定為'single'。
>>> #流程語句使用exec
>>> code1 = 'for i in range(0,10): print (i)'
>>> compile1 = compile(code1,'','exec')
>>> exec (compile1)
0
1
2
3
4
5
6
7
8
9


>>> #簡單求值表達式用eval
>>> code2 = '1 + 2 + 3 + 4'
>>> compile2 = compile(code2,'','eval')
>>> eval(compile2)
10


>>> #交互語句用single
>>> code3 = 'name = input("please input your name:")'
>>> compile3 = compile(code3,'','single')
>>> name #執行前name變數不存在
Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    name
NameError: name 'name' is not defined
>>> exec(compile3) #執行時顯示交互命令,提示輸入
please input your name:'pythoner'
>>> name #執行後name變數有值
"'pythoner'"

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 大家好!今天帶來的是自己實現的用C++實現的10以內整數的科學計算器,其中涉及中綴表達式到尾碼表達式(逆波蘭表示法),尾碼表達式的求值,涉及棧這一數據結構的壓棧,彈棧,存取棧頂元素和判斷棧是否為空等操作. 計算器在生活中應用廣泛.眾所周知,我們往計算器中輸入的是由數字,運算符組成的表達式,這個表達式 ...
  • 內部類不是很好理解,但說白了其實也就是一個類中還包含著另外一個類 如同一個人是由大腦、肢体、器官等身體結果組成,而內部類相當於其中的某個器官之一,例如心臟:它也有自己的屬性和行為(血液、跳動) 顯然,此處不能單方面用屬性或者方法表示一個心臟,而需要一個類 而心臟又在人體當中,正如同是內部類在外部內當 ...
  • 首先看這兩類都實現List介面,而List介面一共有三個實現類,分別是ArrayList、Vector和LinkedList。List用於存放多個元素,能夠維護元素的次序,並且允許元素的重覆。3個具體實現類的相關區別如下: 查看Java源代碼,發現當數組的大小不夠的時候,需要重新建立數組,然後將元素 ...
  • 前言 項目的模塊化有利於任務分工,後期維護,易擴展,模塊還可以獨立成服務單獨部署等; 創建packaging類型為POM的父項目 我用的maven插件是m2e,相信大部分人在eclipse裝的也是m2e插件;廢話不說,直接開始: 菜單選擇新建maven project,註意選擇創建一個簡單工程,如下 ...
  • 一、複製影片剪輯 1.方法:duplicatemovieClip(影片實名,新實名,深度級別) 2.解釋:影片實名就是你要複製的對象,新實名就是你要粘貼的對象,深度級別就是粘貼後的影片剪輯的堆疊順序(頂層、底層等)。 3.例如:我們先繪製一個影片剪輯,其實例名設為mc,再繪製一個按鈕,在該按鈕上添加 ...
  • 引入別人的項目發現利用HibernateTemplate的load的方法報錯了。錯誤提示為: The method load(Class, Serializable) in the type HibernateTemplate is not applicable for the arguments ...
  • Spring AOP應用:xml配置及註解實現。 動態代理:jdk、cglib、javassist 緩存應用:高速緩存提供程式ehcache,頁面緩存,session緩存 項目地址:https://github.com/windwant/spring-aop-test ...
  • 記得當初自己剛開始學習Java的時候,對Java的IO流這一塊特別不明白,所以寫了這篇隨筆希望能對剛開始學習Java的人有所幫助,也方便以後自己查詢。Java的IO流分為字元流(Reader,Writer)和位元組流(InputStream,OutputStream),位元組流顧名思義位元組流就是將文件的 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...