如何使用封閉類別的型別對方法進行型別提示?
Python 中使用型別提示來指示變數的預期型別或函數的回傳值。它們提供了一種記錄程式碼預期行為的方法,並可以幫助及早捕獲錯誤。
問題陳述:
您在Python 3 中有以下程式碼:
class Position: def __init__(self, x: int, y: int): self.x = x self.y = y def __add__(self, other: Position) -> Position: return Position(self.x + other.x, self.y + other.y)
但是,您的編輯器(PyCharm) 會標記一個錯誤,表示在類型提示中對Position 的參考__add__ 無法解析。這就提出了一個問題:如何指定回傳類型應該是 Position 類型?
解決方案:
在Python 中,類型提示主要有三種方法具有其封閉類型類型的方法,取決於您使用的Python 版本:
Python 3.11 :
from typing import Self class Position: def __add__(self, other: Self) -> Self: ...
帶有 from __future__ 導入註解的 Python 3.7:
from __future__ import annotations class Position: def __add__(self, other: Position) -> Position: ...
Python 3.和早期:
class Position: def __add__(self, other: 'Position') -> 'Position': ...
說明:
- Python 3.11 : 引入了一個新的類型註釋專門設計的用於引用封閉的類型類別。
- 帶有 from __future__ import 註解的 Python 3.7: 啟用類型註解的延遲評估,將它們儲存為字串,直到模組完全載入。
- Python 3.6 及更早版本: 類型中使用類別名稱的字串表示形式hint.
預編譯要求:
預編譯要求:
在3.7 之前的Python在版本中,類型提示中使用字串需要定義所引用的類別在類型註解中使用之前。否則,您將遇到 NameError。
注意事項:- 在 Python 3.11 中首選使用 Self,因為它提供了一種更明確且可讀的方式來引用封閉類型。
- 在 Python 3.7 及更高版本中,from __future__ import註解可以顯著提高導入輸入模組的程式碼的效能。
以上是如何在 Python 中使用封閉類別類型來型別提示方法?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonlistsareimplementedasdynamicarrays,notlinkedlists.1)他們areStoredIncoNtiguulMemoryBlocks,mayrequireRealLealLocationWhenAppendingItems,EmpactingPerformance.2)LinkesedlistSwoldOfferefeRefeRefeRefeRefficeInsertions/DeletionsButslowerIndexeDexedAccess,Lestpypytypypytypypytypy

pythonoffersFourmainMethodStoreMoveElement Fromalist:1)刪除(值)emovesthefirstoccurrenceofavalue,2)pop(index)emovesanderturnsanelementataSpecifiedIndex,3)delstatementremoveselemsbybybyselementbybyindexorslicebybyindexorslice,and 4)

toresolvea“ dermissionded”錯誤Whenrunningascript,跟隨台詞:1)CheckAndAdjustTheScript'Spermissions ofchmod xmyscript.shtomakeitexecutable.2)nesureThEseRethEserethescriptistriptocriptibationalocatiforecationAdirectorywherewhereyOuhaveWritePerMissionsyOuhaveWritePermissionsyYouHaveWritePermissions,susteSyAsyOURHomeRecretectory。

ArraysarecrucialinPythonimageprocessingastheyenableefficientmanipulationandanalysisofimagedata.1)ImagesareconvertedtoNumPyarrays,withgrayscaleimagesas2Darraysandcolorimagesas3Darrays.2)Arraysallowforvectorizedoperations,enablingfastadjustmentslikebri

ArraySaresificatificallyfasterthanlistsForoperationsBenefiting fromDirectMemoryAcccccccCesandFixed-Sizestructures.1)conscessingElements:arraysprovideconstant-timeaccessduetocontoconcotigunmorystorage.2)iteration:araysleveragececacelocality.3)

ArraySareBetterForlement-WiseOperationsDuetofasterAccessCessCessCessCessCessCessCessAndOptimizedImplementations.1)ArrayshaveContiguucuulmemoryfordirectAccesscess.2)列出sareflexible butslible butslowerduetynemicizing.3)

在NumPy中进行整个数组的数学运算可以通过向量化操作高效实现。1)使用简单运算符如加法(arr 2)可对数组进行运算。2)NumPy使用C语言底层库,提升了运算速度。3)可以进行乘法、除法、指数等复杂运算。4)需注意广播操作,确保数组形状兼容。5)使用NumPy函数如np.sum()能显著提高性能。

在Python中,向列表插入元素有兩種主要方法:1)使用insert(index,value)方法,可以在指定索引處插入元素,但在大列表開頭插入效率低;2)使用append(value)方法,在列表末尾添加元素,效率高。對於大列表,建議使用append()或考慮使用deque或NumPy數組來優化性能。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載
最受歡迎的的開源編輯器