如何从内部类访问外部类? [英] How to access outer class from an inner class?

查看:155
本文介绍了如何从内部类访问外部类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的情况...

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.

这篇关于如何从内部类访问外部类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆