python - 需要另一个静态方法的 Python 类静态方法
问题描述
是否可以有多个相互调用的静态方法。
就像是:
class Myclass():
def __init__(self, a):
self.var = self.static1(a)
@staticmethod
def static1(i):
i += 1
return self.static2(i)
@staticmethod
def static2(i):
return i * 3
c = Myclass(1)
我有 :
NameError: name 'self' is not defined
解决方案
使用@classmethod
装饰器函数将表示它不会更改实例中的任何内容,而您可以通过第一个参数访问该类cls
class Myclass():
def __init__(self, a):
self.var = self.static1(a)
@classmethod
def static1(cls,i):
i += 1
return cls.static2(i)
@staticmethod
def static2(i):
return i * 3
推荐阅读
- sql-server - “MSXML2.ServerXMLHTTP”请求在 SQL Server 中返回 NULL
- c - uint32_t * uint32_t = uint64_t 向量乘法与 gcc
- java - 从 Hadoop 的 MultipleInputs 和 MultipleOutputs 生成单个文件
- data-structures - 代码部队 607A。得到错误的答案
- python - 在 pandas 和 numpy 中恢复丢失的行和列名
- node.js - 在MVC中,使用ORM时我只需要模型吗?
- python - 图像搜索在使用 pyautogui python 的不同系统中不起作用?
- java - Spring boot 未找到线程绑定请求
- assembly - mips uasm代码有什么优势
- sql - 用十进制值更新 SQL 表只会产生 0