Python 2.x - 在同一个类中创建类的静态实例

时间:2013-04-30 11:04:52

标签: python class static visibility definition

我正在尝试在python中编写一个“枚举类”。我目前遇到的一个小麻烦是无法在枚举类中定义枚举值。也就是说,我可以这样做:

class Fruit:
    def __init__(self, name):
        self.name = name

class Fruits(Enum):
    Apple = Fruit("apple")

但我想这样做,或者是一个类似的清晰的等同物:

class Fruit(Enum):
    def __init__(self, name):
        self.name = name

    Apple = Fruit("apple")

不幸的是,我收到以下错误:    名称'Fruit'未定义

在这种情况下,可见度的规则是什么?是否有任何鲜为人知的Python技巧可以帮助我?我更喜欢可以在Enum的元类中编写的东西,因为这样可以减少用户使用的麻烦。

1 个答案:

答案 0 :(得分:1)

你可以创建一个像这样简单的元类:

class MetaEnum(type):
    def __new__(cls, class_name, parents, attrs):
        def __init__(self, name=None):
            if name is not None: self.name = name
        attrs['__init__'] = __init__
        Klass = type.__new__(cls, class_name, parents, attrs)
        if 'instances' in attrs:
            for name in attrs['instances']:
                setattr(Klass, name.capitalize(), Klass(name))
            del Klass.instances # clean up
        return Klass

class Fruit(object):
    __metaclass__ = MetaEnum
    instances = ('apple', 'banana', 'cranberry')

for attr_name in dir(Fruit):
    if not attr_name.startswith('_'):
        attr = getattr(Fruit, attr_name)
        if type(attr) is Fruit:
            print('Fruit.{}, is a Fruit named {}'.format(attr_name, getattr(attr, 'name')))
        else:
            print('Fruit.{}, is a {}'.format(attr, type(attr)))

输出:

Fruit.Apple, is a Fruit named apple
Fruit.Banana, is a Fruit named banana
Fruit.Cranberry, is a Fruit named cranberry