在pythonoop中,从模块it'初始化类可以吗;进口时所含的

在pythonoop中,从模块it'初始化类可以吗;进口时所含的,python,oop,module,Python,Oop,Module,我正在使用Python 2.7 我想知道在Python OOP中,是否可以在模块级添加代码来初始化模块中包含的类 class DoSomething(object): foo = 0 bar = 0 @classmethod def set_all_to_five(cls): cls.bar = 5 cls.foo = 5 @classmethod def set_all_to_ten(cls):

我正在使用Python 2.7

我想知道在Python OOP中,是否可以在模块级添加代码来初始化模块中包含的类

class DoSomething(object):

    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


#Module level code - runs on import of the class DoSomething
DoSomething.set_all_to_five()
输出:

>>> from productX.moduleY import DoSomething
>>> print DoSomething.bar
5
该类只包含@classmethod方法,因此我可以调用它们,而无需实例化该类

模块级
code DoSomething.set_all_to_5()
在导入模块时初始化类级属性

[是否]可以在模块级别添加代码以初始化模块中包含的类

是的,你有的很好。当人们将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  # for Python3, pass metaclass kwarg instead

    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):
    ....
或者,更简单地说,通过使用装饰器

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  # for Python3, pass metaclass kwarg instead

    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):
    ....

谢谢这是我希望得到的答复。感谢您提供有关元类的更多信息。在我的例子中,我使用模块级代码用一些仅在运行时可用的数据初始化类。