Skip to Content
Python 技術手冊 第三版
book

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate to advanced
856 pages
17h 17m
Chinese
GoTop Information, Inc.
Content preview from Python 技術手冊 第三版
430
|
13 章 執行的控制
動態執行和 exec
Python
exec
述句(v3 中的內建函式)能夠執行你讀取、產生或在程式
執行過程中以其他方式取得的程式碼。
exec
會動態地執行一個述句或一套
suite)述句。在 v2 中,
exec
是一個簡單的關鍵字述句,具有下列語法:
exec code[ in globals[,locals]]
code
可以是一個字串、一個開啟的類檔案物件(file-like object),或一個
程式碼物件(code object)。
globals
locals
是映射(mappings)。 v3
中,
exec
是一個內建函式,具有下列語法:
exec(code, globals=None, locals=None)
code
可以是一個字串、位元組,或程式碼物件。
globals
是一個
dict
locals
是任何映射。
如果
globals
locals
都有出現,它們就是
code
執行的全域和區域命名空
間。如果只出現
globals
exec
就會使用
globals
作為那兩個命名空間。如
果都沒出現,
code
就會在目前的範疇(scope)中執行。
永遠別在目前的範疇中執行
exec
在目前的範疇中執行
exec
是非常糟糕的主意:它可能繫結
bind)、重新繫結(rebind)或解除繫結(unbind)任何
的全域名稱。要讓事情保持控制,若要使用
exec
,就只用
特定的、明確的字典。
避免 exec
有關於 Python,一個常被問到的問題是「我要如何設定其名稱剛剛才讀入
或建立的變數呢?」 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

利用 Python 进行数据分析(原书第2版)

利用 Python 进行数据分析(原书第2版)

Wes McKinney

Publisher Resources

ISBN: 9789864766819