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 技術手冊 第三版
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.

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