cost 200 ms
方法內的默認參數

[英]Default argument inside method

我嘗試為 class 實現格式。我想要一個默認為“短”的參數。 我試過: 最后一次調用引發了 TypeError,但我希望默認為“短”實現。 任何想法為什么? 當然我可以使用類似的東西:if not code or code == 'short' 但我希望我能理解我最初的實現是怎么回事。 ...

pydantic basemodel 破壞了類方法對屬性的訪問

[英]pydantic basemodel breaks classmethod access to attributes

我正在嘗試在 python 中導出 class 的一些屬性。引入pydantic會破壞類方法中對屬性的訪問。 以這個工作示例為例: 如果我將它擴展到pydantic ,它會以某種方式中斷 引發以下錯誤: 有人知道這里發生了什么嗎? 更重要的是,如何克服這個問題? ...

為什么實例變量沒有改變?

[英]Why is the instance variable not getting changed?

Output:Please input a valid integer greter than 0 20 'Player name: Nail. Player age: -25' 變量Age沒有變化。 它仍保留其最初的 state。 ...

如何創建具有某些屬性的 Student 類並稍后從其中的方法調用它

[英]how to create Student class with some attributes and calling it later from method within

有必要創建一個模板來創建 Student 類型的對象。 每個學生應具有以下特征:姓名、地址、電話、課程、索引號 每個代表學生的對象都應具有以下方法: get info() getInfo() 方法應打印有關一名學生的數據(例如姓名:Jon , 地址:JonJon 電話:1231 學科:數學 索引號: ...

是否有關於裝飾器的 DRY 規則?

[英]Is there a DRY rule regarding decorators?

我的印象是,如果你在一個類中連續使用兩個或多個相同種類的裝飾器方法,比如@classmethod,你不必兩次使用@classmethod(DRY原則)。 例如,如果你使用@classmethod 並且類中定義的下一個方法也有 cls 作為第一個隱式參數,那么 Python 的解釋器應該知道第二個方 ...

在Python中創建一個class的屬性,使用實例的class方法調用self.attribute。 AttributeError:類型 object 沒有屬性

[英]Create an attribute of a class in Python that uses class method of the instance to call on self.attribute. AttributeError:type object has no attribute

我是 Python 的初學者,但我之前編程過一點,並且在 Java 上過一些課程。 我試圖了解如何在 python 中使用類。我正在使用 spotify api 從 api 獲取藝術家的信息。我創建了一個 class,我在其中定義了init和 @classmethod get_spapi,或者獲取 ...

如何創建兩個 class 方法並在另一個方法的 if 語句上使用第一個 class 方法變量 - Java

[英]how to create two class methods and using the first class methods variables on an if statement on the other method - Java

所以我正在嘗試創建一個程序來顯示它所生活的恐龍時代和其他東西,但我目前停留在這個時代。 這是問題: setTimePeriod (方法)——采用一個整數參數來表示恐龍生活在幾百萬年前,並且不返回任何內容; 將時間段實例變量設置為時間段的中間。 請參閱下面的詳細信息 getTimePeriod ( ...

為什么實例化 class 的一個實例會更改在另一個實例上調用的方法的結果?

[英]Why can instantiating one instance of a class change the result of a method called on another instance?

當我嘗試使用教科書中的示例並嘗試重新排列一些代碼行以執行時,我發現結果不是我所期望的。 有一個“類”,如下所示。 當我執行它時...... 在我看來,c1 和 c2 都應該是獨立的對象。 代碼如何導致 c1 的 1021.01675 和 c2 的 1021.01675 但..... 我是否忽視、忘 ...

類方法的實現

[英]Implementation of classmethod

從 python 文檔中,classmethod 大致相當於 我的問題是:在什么情況下,類是無。 我用測試 class 進行了測試 在這些情況下,類是 <class ' main .Test'>。 ...

初始化前無法訪問“rainDrop”

[英]Cannot access 'rainDrop' before Initialization

我正在使用類和面向對象的編程(我不習慣)在 Sublime Text 中創建一個 Purple Rain Sim。 當我嘗試在下面的代碼中創建新的 object 時,我的 chrome 控制台會拋出錯誤“初始化前無法訪問 'rainDrop'”。 'rainDrop' 是一個創建雨滴對象的 cl ...

create() 方法是否將 model object 保存到 Django 數據庫?

[英]Does create() method save model object to Django database?

我剛剛在 Django 模型中使用create方法models.Model子類。 文檔有這個示例代碼: 這是否已經將Book object 保存到數據庫? 我的直覺建議save()方法應該在# do something with the book的行上調用,否則它不應該工作。 但是,當我嘗試對其 ...

具有繼承的 Python 類方法

[英]Python classmethod with inheritance

有人可以向我解釋如何在 Python 中正確使用類方法和繼承嗎? 我需要創建兩個classmethod 方法: 做全職員工。 在繼承自 BaseEmployee 的 Employee 類中創建兼職員工。 似乎我完全不明白一個概念:( 好的,那么問題是如何正確創建一個類 ...

不要控制兩個玩家玩

[英]Don't work controls to play for two players

方法調用 我想控制兩個玩家:方向鍵和'wasd'。 但只有最后調用的方法有效,即setControlsArrow() 。 請幫忙,如何在 vanila js 中為兩個玩家制作控件? ...


 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM