【发布时间】:2019-11-04 06:27:57
【问题描述】:
我是 python 新手。我听说在 python 中,无论是类还是函数,一切都是对象。
据我所知,类对象是在程序启动时创建的。所以我想知道是否有一些用于初始化类变量或做某事的函数。
init 函数不能这样做,因为它的参数是 self ,它是类的一个实例。我想访问类本身。
class Example():
def __init__(self):
# I know that this function is called when an instance of this class is created
pass
def __something__(cls):
# Is there some function for initializing class object?
# What I wanna do is I want to initialize class variables or call some functions...
# But __init__ function can't do because its parameter is self.
pass
【问题讨论】:
-
只需在
class定义中执行cls_attr = 'Foo'...!? -
是的,您正在寻找metaclass
标签: python python-3.x python-2.7