Python 方法的嵌套类访问外部类\uuu init__

Python 方法的嵌套类访问外部类\uuu init__,python,class,oop,inner-classes,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

我试图创建一个结构,在这个结构中我定义了一组常量(这里是一个文件和一些元数据),这些常量被存储起来,这样我就可以运行分组到子类中的方法

以下是我的结构:

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 

diagnostics.io
diagnostics
无关,只存在于其名称空间中,它当然与它的任何实例无关。重新思考你想做什么。

詹姆斯所做的几乎像是RequireJS的习语。所以我认为这是不可能的?因此,我必须将它们声明为全局的,或者每次都重新定义它们,而不是封装它们?如果这些值是静态的,那么将它们存储在类实例中是没有意义的。只需使用类属性。此外,您还可以将属性直接粘贴到
io
类上。