Python:如何从内部静态类中获取外部类变量? [英] Python: How to get Outer class variables from inner static class?

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

问题描述

我想通过使实例Outer(variable)来一次指定变量,而不是在所有静态类中都使用此变量,我该怎么做?除了使用非静态方法并将Outer传递到每个内部类之外,还有其他解决方案吗?

I want to specify variable once by making instance Outer(variable), than this variable use in all static classes, how should I do that? Is there any other solution than use not static methods and pass Outer into each inner class?

class Outer():
    def __init__(self, variable):
        self.variable= variable

    class Inner1():        
        @staticmethod
        def work1():
            **print Outer.variable**

    class Inner2():        
        @staticmethod
        def work2():
            **print Outer.variable**

推荐答案

否.内部类方法无法访问外部类的实例.

No. Inner-class methods have no way of accessing instances of the outer class.

这篇关于Python:如何从内部静态类中获取外部类变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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