」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 在Python中如何在一個類內從一個函數調用另一個函數?

在Python中如何在一個類內從一個函數調用另一個函數?

發佈於2025-05-02
瀏覽:742

在類中調用函數:一種實用方法How Do I Call One Class Function from Another Within the Same Class in Python?

The Issue:

Consider the following code that calculates distances between coordinates:

class Coordinates: DEF DISTOPONT(self,p): #使用畢達哥拉斯定理計算距離 def isnear(self,p): #我們如何調用ISNEAR的DistTopoint?

在此示例中,我們要使用DistTopoint函數確定一個點是否在另一點附近。我們如何在ISNEAR函數中調用此函數? 解決方案:在同一類中調用成員函數,我們需要使用類的實例(通過慣例稱為自我稱為自我)來訪問其方法。更正的ISNEAR函數看起來像這樣:

class coordinates: DEF DISTOPONT(self,p): #使用畢達哥拉斯定理計算距離 def isnear(self,p): self.disttopoint(p) #繼續在脫機之前添加自我,繼續進行其他操作

,我們明確表示我們要在坐標類的實例上調用成員函數disttopoint。那個實例:

coordinates = coordinates() coordinates.isnear(另一個_point)
這將使用distTopoint函數計算實例坐標與指定的另一_point之間的距離。 [&& &&&&華
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3