如何从内部类访问外部类?

问题描述:

我有这样的情况...

I have a situation like so...

class Outer(object):

    def some_method(self):
        # do something

    class Inner(object):
        def __init__(self):
            self.Outer.some_method()    # <-- this is the line in question

如何从Inner类访问Outer类的方法?

How can I access the Outer class's method from the Inner class?

嵌套类的方法无法直接访问外部类的实例属性.

The methods of a nested class cannot directly access the instance attributes of the outer class.

请注意,即使您已经创建了内部类的实例,也不一定存在外部类的实例.

Note that it is not necessarily the case that an instance of the outer class exists even when you have created an instance of the inner class.

实际上,通常建议不要使用嵌套类,因为嵌套并不暗示内部类和外部类之间的任何特定关系.

In fact, it is often recommended against using nested classes, since the nesting does not imply any particular relationship between the inner and outer classes.