如何在构造函数之前声明静态变量,如何在其体中引用类?

时间:2012-04-14 19:28:02

标签: python oop class

我曾经在构造函数之前声明类中的静态字段/变量。在Python中执行此操作时会导致错误。

这是一个示例课程:

class StringCompare:

    methods = OrderedDict()
    # ERROR!:
    #methods['equals'] = equals
    #methods['ends with'] = endswith
    #methods['starts with'] = startswith
    #methods['contains'] = contains

    @staticmethod
    def equals(a, b):
        return a == b

    @staticmethod
    def contains(a, b):
        return a.find(b) != -1

    @staticmethod
    def startswith(a, b):
        return a.startswith(b)

    @staticmethod
    def endswith(a, b):
        return a.endswith(b)

    methods['equals'] = equals
    methods['ends with'] = endswith
    methods['starts with'] = startswith
    methods['contains'] = contains

还有更优雅的方法(除了在整个类之前直接放置所有语句,每个访问的变量前缀为StringCompare.)?

这里的最佳做法是什么?


更复杂的情况是尝试从同一个类中调用构造函数时:

class Type(InlineFragment):

    # primitive types get None as package name
    def __init__(self, packageName, name, genericType=None):

        ...

    def ...

    primitive = {
        'Character': Type(None, 'char'),
        'Byte'     : Type(None, 'byte'),
        'Short'    : Type(None, 'short'),
        'Integer'  : Type(None, 'int'),
        'Long'     : Type(None, 'long'),
        'Boolean'  : Type(None, 'boolean'),
        'Float'    : Type(None, 'float'),
        'Double'   : Type(None, 'double'),
    }

这会导致错误:

\jpa_export_fragments.py", line 361, in Type
    'Character'    : Type(None, 'char'),
NameError: name 'Type' is not defined

这应该可行,但我只能通过将此代码放在类之外来解决这个问题。

1 个答案:

答案 0 :(得分:3)

通常,解决方案是使用class decorators。对于您的示例,您可能希望将它们与类方法结合使用:

def apply_method(attr):
    def apply_to(cls):
        setattr(cls, attr, getattr(cls, '_' + attr)())
        return cls
    return apply_to

@apply_method('primative')
class Type(object):

    def __init__(self, *args):
        pass

    @classmethod
    def _primative(cls):
        return {
    'Character': cls(None, 'char'),
    'Byte'     : cls(None, 'byte'),
    'Short'    : cls(None, 'short'),
    'Integer'  : cls(None, 'int'),
    'Long'     : cls(None, 'long'),
    'Boolean'  : cls(None, 'boolean'),
    'Float'    : cls(None, 'float'),
    'Double'   : cls(None, 'double'),
        }

你的第一个例子看起来非常不像Pythonic,所以我不愿意为此建议一个装饰者。相反,也许你想要一个str子类?

class StringCompare(str):
    # none of these are any different from the normal string operations
    # you would really only override ones that are different.

    def __eq__(self, other):
        return super(StringCompare, self).__eq__(other)

    def __contains__(self, other):
        return self.find(other) != -1

    def startswith(self, other):
        return super(StringCompare, self).startswith(other)

    def endswith(self, other):
        return super(StringCompare, self).endswith(other)


print StringCompare('boogaloo').startswith('boo') # True