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

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate to advanced content levelIntermediate to advanced
856 pages
17h 17m
Chinese
GoTop Information, Inc.
Content preview from Python 技術手冊 第三版
750
|
24 章 擴充與內嵌標準型的 Python
實作一個型別最佳的方式是從目錄
Modules
中的三個檔案之一複製 Python
原始碼並編輯之,Python 就是為了這種目的才提供它們的。這些檔案的
名稱為
xxlimited.c
(僅限 v3)、
xxmodule.c
,以及
xxsubtype.c
(後者專注
於衍生內建型別的子類別,有兩個型別,每個各自從
list
dict
衍生
子類別)。
參閱線上文件(
https://docs.python.org/3/c-api/typeobj.html
)來找
PyTypeObject
與其他相關 structs 的詳細說明。Python 原始碼中的檔案
Include/object.h
含有這些型別的宣告(declarations),連同幾個你會想要好
好讀的重要註解。
專屬每個實體的資料(per-instance data
要表示你型別的實體,就宣告一種 C struct,緊接在左大括號(opening
brace)後,以巨集
PyObject_HEAD
開頭。這個巨集會展開為你的 struct
了成為一個 Python 物件必須具備的資料欄位。這些欄位包括參考計數
reference count)和對該實體之型別的一個指標。對你結構(structure
的任何指標都能正確地被轉型(cast)為一個
PyObject*
。你可以選擇將這
種實務做法視為(單一)繼承機制的一種 C 層級的實作。
定義你型別的特徵與行為的
PyTypeObject
struct 必須含有你各實體 ...
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.
Start your free trial

You might also like

測試驅動開發|使用 Python

測試驅動開發|使用 Python

Harry J.W. Percival
Linux 内核观测技术BPF

Linux 内核观测技术BPF

David Calavera, Lorenzo Fontana

Publisher Resources

ISBN: 9789864766819