python类的构造函数是,Python类构造函数(静态)

Does Python have a mechanism for class constructors, i.e. a function that is called whenever the class is first referenced (as opposed to when an instance of that object is created)? I know this exists in some other languages, but I haven't come across it in Python.

Basically, I would like to initialise some static attributes in that function. I put an example below of what I would expect. Of course, the example returns None, but I would like it return 'foo'.

class T:

arg = None

def __class_constructor__():

T.arg = 'foo'

print(T.arg) # returns None

To avoid confusion: I am well aware of the object constructor, but that's not what I want, because it is only called once the first object is created, not before:

class T:

arg = None

def __init__(self):

type(self).arg = 'foo'

print(T.arg) # returns None

obj = T()

print(T.arg) # returns 'foo'

解决方案

You can use a class decorator:

def add_arg(cls):

if not hasattr(cls, "arg"):

cls.arg = 'foo'

return cls

@add_arg

class T(object):

pass

Or a custom metaclass:

class WithArg(type):

def __new__(meta, name, bases, attrs):

cls = type.__new__(meta, name, bases, attrs)

if not hasattr(cls, "arg"):

cls.arg = "foo"

return cls

# python 2

class T(object):

__metaclass__ = WithArg

# python 3

class T(metaclass=WithArg):

pass

But as others already mention this won't give you much more than plainly setting the class attribute in the class statement.

NB : if you want a computed attribute on the class itself, you'll have to either set it as a property on a custom metaclass

class WithProp(type):

@property

def arg(cls):

return "foo"

class T(object):

__metaclass__ = WithProp

T.arg

=> 'foo'

But arg will only be available on the class object itself, not on it's instances:

T().arg

Traceback (most recent call last):

File "", line 1, in

AttributeError: 'T' object has no attribute 'arg'

or write your own custom descriptor:

class ArgDescriptor(object):

def __get__(self, obj, cls=None):

return 42

class T(object):

arg = ArgDescriptor()

T.arg

=> 42

T().arg

=> 42

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值