![](/img/trans.png)
[英]Default argument inside method
我嘗試為 class 實現格式。我想要一個默認為“短”的參數。 我試過: 最后一次調用引發了 TypeError,但我希望默認為“短”實現。 任何想法為什么? 當然我可以使用類似的東西:if not code or code == 'short' 但我希望我能理解我最初的實現是怎么回事。 ...
[英]Default argument inside method
我嘗試為 class 實現格式。我想要一個默認為“短”的參數。 我試過: 最后一次調用引發了 TypeError,但我希望默認為“短”實現。 任何想法為什么? 當然我可以使用類似的東西:if not code or code == 'short' 但我希望我能理解我最初的實現是怎么回事。 ...
[英]pydantic basemodel breaks classmethod access to attributes
我正在嘗試在 python 中導出 class 的一些屬性。引入pydantic會破壞類方法中對屬性的訪問。 以這個工作示例為例: 如果我將它擴展到pydantic ,它會以某種方式中斷 引發以下錯誤: 有人知道這里發生了什么嗎? 更重要的是,如何克服這個問題? ...
[英]Defered invocation of a python classmethod obtained in a decorator
我有一個裝飾器用來包裝這樣的類方法:class Class(object): @register_classmethod @classmethod def my_class_method(cls): ... 我的裝飾器得到一個classmethod當我 ...
[英]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。 ...
[英]how to create Student class with some attributes and calling it later from method within
有必要創建一個模板來創建 Student 類型的對象。 每個學生應具有以下特征:姓名、地址、電話、課程、索引號 每個代表學生的對象都應具有以下方法: get info() getInfo() 方法應打印有關一名學生的數據(例如姓名:Jon , 地址:JonJon 電話:1231 學科:數學 索引號: ...
[英]Is there a DRY rule regarding decorators?
我的印象是,如果你在一個類中連續使用兩個或多個相同種類的裝飾器方法,比如@classmethod,你不必兩次使用@classmethod(DRY原則)。 例如,如果你使用@classmethod 並且類中定義的下一個方法也有 cls 作為第一個隱式參數,那么 Python 的解釋器應該知道第二個方 ...
[英]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,或者獲取 ...
[英]how to create two class methods and using the first class methods variables on an if statement on the other method - Java
所以我正在嘗試創建一個程序來顯示它所生活的恐龍時代和其他東西,但我目前停留在這個時代。 這是問題: setTimePeriod (方法)——采用一個整數參數來表示恐龍生活在幾百萬年前,並且不返回任何內容; 將時間段實例變量設置為時間段的中間。 請參閱下面的詳細信息 getTimePeriod ( ...
[英]Applying class decorator to staticmethod: takes 1 positional argument but 2 were given
我正在嘗試將 class 裝飾器應用於其每個方法。 但是staticmethod和classmethod對我不起作用。 我懂了:import functools import re def decor(cls): def decorator(f): if isinsta ...
[英]Python question about overridden/redifined imported function as Class method
在https://github.com/biopython/biopython/blob/518c4be6ae16f1e00bfd55781171da91282b340a/Bio/SeqUtils/ProtParam.py我有這個導入語句: from Bio.SeqUtils import mole ...
[英]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 但..... 我是否忽視、忘 ...
[英]I have a Typescript class where a method is not recognized
我有一個 Typesript class,AjaxBookStringParams。 當我嘗試使用函數/方法 BuildGenieParams 時,錯誤:“BookGenieLoader.ts:47 Uncaught TypeError: ajaxString.BuildGenieParams is ...
[英]How to, in Python3, make the typing system know a subclass instance created by `super().*` method has subclass methods
在上面的示例中,類型檢查器抱怨子類create方法中的do方法調用。 有沒有辦法解決這個問題? ...
[英]Implementation of classmethod
從 python 文檔中,classmethod 大致相當於 我的問題是:在什么情況下,類是無。 我用測試 class 進行了測試 在這些情況下,類是 <class ' main .Test'>。 ...
[英]Given a classmethod reference, how to get a reference to the class in which it is defined?
我在代碼中的某個點有一個綁定的類方法引用。 我想從中獲取 class 本身。 我該怎么做? ...
[英]Cannot access 'rainDrop' before Initialization
我正在使用類和面向對象的編程(我不習慣)在 Sublime Text 中創建一個 Purple Rain Sim。 當我嘗試在下面的代碼中創建新的 object 時,我的 chrome 控制台會拋出錯誤“初始化前無法訪問 'rainDrop'”。 'rainDrop' 是一個創建雨滴對象的 cl ...
[英]Does create() method save model object to Django database?
我剛剛在 Django 模型中使用create方法models.Model子類。 文檔有這個示例代碼: 這是否已經將Book object 保存到數據庫? 我的直覺建議save()方法應該在# do something with the book的行上調用,否則它不應該工作。 但是,當我嘗試對其 ...
[英]self.__class__ in parent class' method is missing arguments
我正在使用的包包含類 A。我希望我的新類 B 繼承自該類。 最小的例子: 在孩子身上調用父母的方法會拋出: 如何在不更改 A 類中的任何內容的情況下解決此問題? 非常感謝您的幫助! ...
[英]Python classmethod with inheritance
有人可以向我解釋如何在 Python 中正確使用類方法和繼承嗎? 我需要創建兩個classmethod 方法: 做全職員工。 在繼承自 BaseEmployee 的 Employee 類中創建兼職員工。 似乎我完全不明白一個概念:( 好的,那么問題是如何正確創建一個類 ...
[英]Don't work controls to play for two players
方法調用 我想控制兩個玩家:方向鍵和'wasd'。 但只有最后調用的方法有效,即setControlsArrow() 。 請幫忙,如何在 vanila js 中為兩個玩家制作控件? ...