嵌套类方法访问外部类__init__

时间:2014-07-14 15:34:15

标签: python class oop inner-classes

我正在尝试创建一个结构,在该结构中我定义了一组常量(这里是一个文件和一些元数据),这些常量被存储以便我可以运行分组为子类的方法。

这是我的结构:

class diagnostics(object):
    # set up some shared attributes which are always constant
    def __init__(self,root_dir, file, year, doy):

       self.root_dir = root_dir
       self.file = file
       self.year = year
       self.doy = doy

    # classes of different methods that
    # require these attributes from diagnostics.__init__

     class io:
        def loadfile()
         # loads the file from diagnostics.__init__.file

所以我希望能够做到这样的事情:

diag = diagnostics('.', 'file01.txt', '2007', '001')
diag.io.loadfile() # will load the file 

1 个答案:

答案 0 :(得分:2)

diagnostics.iodiagnostics除了在其命名空间中存在之外没有任何关系,它当然与它的任何实例无关。重新思考你想要做的事情。

相关问题