2013-12-11 124 views
0

我有这样的代码:嵌套类和静态方法

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是不确定的。它怎么可能是未定义的?为什么不ServicerSrvOne

回答

5

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

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

为什么Servicer不是一个模块?

+0

谢谢。对,可能应该只是一个模块。 –