[It it] OK to add module level code to initialize the class contained in the module?
, . , "dynamic" , Python : . , , , DoSomething, - "" .
, , , , "monkeypatch" , .
:
class DoSomethingMeta(type):
def __init__(self, name, bases, attrs):
super(DoSomethingMeta, self).__init__(name, bases, attrs)
self.set_all_to_five()
class DoSomething(object):
__metaclass__ = DoSomethingMeta
foo = 0
bar = 0
@classmethod
def set_all_to_five(cls):
cls.bar = 5
cls.foo = 5
@classmethod
def set_all_to_ten(cls):
cls.bar = 10
cls.foo = 10
, , :
def mydecorator(cls):
cls.set_all_to_five()
return cls
@mydecorator
class DoSomething(object):
....