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 技術手冊 第三版
終止函式
|
429
不匯入
user
的指令稿不會執行
.pythonrc.py
Python v3 的指令稿也不會,
因為
user
模組在 v3 中並沒有定義。當然,任何給定的指令稿都能自由安
排其他特定的方式來執行它所需要的、使用者限定的起始模組。這種應
用程式限定的安排方式,即使在 v2 中,也比匯入
user
還要常見。透過
import user
所載入的一個通用的
.pythonrc.py
,必須是載入它的任何應用程
式都能使用的。特殊化、應用程式限定的起始檔案(startup files)只需要
遵循特定的應用程式以文件記載的慣例就行了。
舉例來說,你的應用程式
MyApp.py
可能在文件中記載它會在使用者的家目
錄中尋找名為
.myapprc.py
的一個檔案,如環境變數
HOME
所指示的,並在
應用程式的主指令稿的全域命名空間中載入它。然後你的主指令稿中可能
就會有下列程式碼:
import os
homedir = os.environ.get('HOME')
if homedir is not None:
userscript = os.path.join(homedir, '.myapprc.py')
if os.path.isfile(userscript):
with open(userscript) as f:
exec(f.read())
在這種情況中,
.myapprc.py
這個使用者自訂指令稿,若有出現,就必須只
處理
MyApp
限定的使用者自訂工作。這種做法比仰賴
user
模組還要好,而
且在 v3 ...
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