gpt4 book ai didi

python - 如何在python中引用类本身

转载 作者:太空宇宙 更新时间:2023-11-04 06:50:44 24 4
gpt4 key购买 nike

<分区>

我想在 python 中有这样的简单类常量:

class FooBarBaz:
BAR = 123

@staticmethod
def getBar():
return BAR # this would not work, of course

return FooBarBaz.BAR # this would work but is "way too long"

有没有更短的方法从方法内部引用类本身,而不是当前实例?它不仅适用于静态方法,而且通常适用于 __class__ 关键字或其他内容。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com