gpt4 book ai didi

python - 在静态方法中引用自己的类

转载 作者:行者123 更新时间:2023-11-28 19:37:23 24 4
gpt4 key购买 nike

在静态方法中引用自己的类有简写吗?

假设我有这段代码:

class SuperLongClassName(object):

@staticmethod
def sayHi():
print 'Hi'

@staticmethod
def speak():
SuperLongClassName.sayHi() # Is there a shorthand?

最佳答案

是的,使用 @classmethod 而不是 @staticmethod@staticmethod 的重点是在不需要时删除额外的类参数。

class SuperLongClassName(object):

@classmethod
def sayHi(cls):
print 'Hi'

@classmethod
def speak(cls):
cls.sayHi()

关于python - 在静态方法中引用自己的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17604316/

24 4 0
文章推荐: javascript - jquery 只允许点与数字
文章推荐: javascript - 如何在打开事件时获取放大弹出 API 的 url?
文章推荐: python - 如何检查 Python 中的函数类型?
文章推荐: python - 美丽汤 :
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com