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 技術手冊 第三版
116
|
3 章 Python 語言
然而,變更遞迴極限並不會讓你有無限制的遞迴,絕對的最大極限取決
於你程式執行的平台,特別是底層的作業系統和 C 的執行階段程式庫
runtime library),而這一般會是數千層深。若是遞迴呼叫過深,你的
程式就會當掉。呼叫
setrecursionlimit
設定了超出平台能力的極限之
後,這種失控的遞迴,是少數幾種會使 Python 程式當掉的可能方式之
一,而且是真正的當掉,沒有一般 Python 例外機制的安全網來保護。
因此,如果你試圖以
setrecursionlimit
提高遞迴極限來「修理」得到
RecursionLimitExceeded
例外的程式,那你要特別小心才行。通常,最好想
辦法移除那個遞迴,或至少限制你程式所需的遞迴深度。
熟悉LispScheme 或函式型程式語言(functional programming
languages)的讀者,必須特別注意 Python
沒有
實作在那些語言中非常
重要的「尾端呼叫消除(tail-call elimination)」的最佳化技巧。在 Python
中,所有的呼叫,不管遞迴與否,就時間和記憶體空間而言,成本都相
同,只取決於引數(arguments)的數量:所需的成本並不會因為呼叫是
一個「尾端呼叫」(「tail-call」,代表該呼叫是呼叫者所執行的最後一項運
算)或非尾端呼叫而有所不同。這使遞迴的移除更顯重要。
舉例來說,考慮遞迴的典型用途:「走遍一個樹狀結構(walking a
tree ...
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