Python 2.7 已終止支援,並將於 2026 年 1 月 31 日
淘汰。淘汰後,您將無法部署 Python 2.7 應用程式,即使貴機構先前曾使用機構政策重新啟用舊版執行階段的部署作業,也無法部署。現有的 Python 2.7 應用程式在
淘汰日期過後,仍會繼續執行並接收流量。建議您
改用系統支援的最新 Python 版本。
訊息模組函式
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
protorpc.messages
套件提供下列位置公用程式函式:
- find_definition(name, relative_to=None)
-
在模組空間中依名稱尋找定義。
尋找演算法會依相對於訊息定義的名稱或依完整名稱來尋找定義。如果找不到與 relative_to
參數相關的定義,系統會搜尋 relative_to
的容器。如果 relative_to
是巢狀訊息,則會搜尋其 message_definition()。如果該訊息沒有 message_definition(),則會搜尋其模組。如果 relative_to
是模組,則尋找演算法會搜尋包含該模組的模組,並相應搜尋。如果該模組為頂層模組,尋找演算法會搜尋使用完整名稱的訊息。如果仍找不到任何訊息,搜尋作業就會失敗,方法會引發 DefinitionNotFoundError。
舉例來說,在搜尋任何 foo.bar.ADefinition
定義 (相對於實際訊息定義 abc.xyz.SomeMessage
) 時:
find_definition('foo.bar.ADefinition', SomeMessage)
這個方法的引數所遵循的格式,與針對完整名稱進行的搜尋相似:
abc.xyz.SomeMessage. foo.bar.ADefinition
abc.xyz. foo.bar.ADefinition
abc. foo.bar.ADefinition
foo.bar.ADefinition
解析相對於 Message 定義和模組的名稱時,演算法會搜尋其路徑中找到的任何 Message 或子模組,並忽略非 Message 的值。
開頭為 '.'
的名稱視為完整合格名稱。尋找演算法會從最頂層的套件開始搜尋。舉例來說,假設有兩種訊息類型:
abc.xyz.SomeMessage
xyz.SomeMessage
搜尋相對於 abc
的 .xyz.SomeMessage
會解析為 xyz.SomeMessage
,而非 abc.xyz.SomeMessage
。對於這類名稱,實際上會將 relative_to 參數忽略,且 relative_to 參數會一律設為 None。
如要進一步瞭解套件名稱解析,請參閱通訊協定緩衝區套件指定碼。
引數
- name
- 要查找的定義名稱。可以是完整名稱或相對名稱。
- relative_to
- 搜尋與訊息定義或模組相關的定義。如果為 None,則會觸發完整名稱的搜尋。
傳回與該名稱相關聯的 Enum 或 Message 類別定義。
如果在所有搜尋路徑中都找不到任何定義,則會發出 DefinitionNotFoundError 錯誤。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-11 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-11 (世界標準時間)。"],[[["\u003cp\u003eThe \u003ccode\u003efind_definition\u003c/code\u003e function in \u003ccode\u003eprotorpc.messages\u003c/code\u003e locates definitions by name, either relative to a message or module or by a fully qualified name.\u003c/p\u003e\n"],["\u003cp\u003eThe search algorithm for \u003ccode\u003efind_definition\u003c/code\u003e prioritizes searching relative to the \u003ccode\u003erelative_to\u003c/code\u003e parameter, progressing up the module hierarchy if necessary and then falling back to searching by fully qualified name.\u003c/p\u003e\n"],["\u003cp\u003eA name starting with \u003ccode\u003e'.'\u003c/code\u003e in \u003ccode\u003efind_definition\u003c/code\u003e is treated as fully qualified, starting the search from the topmost package and ignoring the \u003ccode\u003erelative_to\u003c/code\u003e parameter.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003efind_definition\u003c/code\u003e method requires two arguments: \u003ccode\u003ename\u003c/code\u003e, which can be fully qualified or relative, and \u003ccode\u003erelative_to\u003c/code\u003e, that specifies a definition to start the search from.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003efind_definition\u003c/code\u003e will return an \u003ccode\u003eEnum\u003c/code\u003e or \u003ccode\u003eMessage\u003c/code\u003e class definition, and in case a definition cannot be found, it will raise a \u003ccode\u003eDefinitionNotFoundError\u003c/code\u003e exception.\u003c/p\u003e\n"]]],[],null,[]]