嵌套类和静态方法

时间:2013-12-11 16:41:48

标签: python class

我有这段代码:

class Servicer(object):
    clsVrb = "run"

    class SrvOne(object):
        def __init__(self, name):
            self.name = name

    class SrvTwo(object):
        def __init__(self, name):
            self.name = name

    @staticmethod
    def make_SrvOne(name):
        return SrvOne(name)





test = Servicer.make_SrvOne("Edgar")
print test

但我得到一个例外,即SrvOne未定义。怎么可能未定义?为什么Servicer没有看到SrvOne

1 个答案:

答案 0 :(得分:5)

它在Servicer命名空间中定义,SrvOne中没有本地make_SrvOne,并且没有全局SrvOne

@staticmethod
def make_SrvOne(name):
    return Servicer.SrvOne(name)

为什么Servicer不是一个模块?

相关问题