访问外部类的内部类在Python问题,怎么解决
答案:2 悬赏:80 手机版
解决时间 2021-02-20 04:52
- 提问者网友:捧腹剧
- 2021-02-19 12:24
访问外部类的内部类在Python问题,怎么解决
最佳答案
- 五星知识达人网友:怀裏藏嬌
- 2021-02-19 12:40
You're trying to access Outer's class instance, from inner class
instance. So just use factory-method to build Inner instance and pass
Outer instance to it.
class Outer(object):
def createInner(self):
return Outer.Inner(self)
class Inner(object):
def __init__(self, outer_instance):
self.outer_instance = outer_instance
self.outer_instance.somemethod()
def inner_method(self):
self.outer_instance.anothermethod()
instance. So just use factory-method to build Inner instance and pass
Outer instance to it.
class Outer(object):
def createInner(self):
return Outer.Inner(self)
class Inner(object):
def __init__(self, outer_instance):
self.outer_instance = outer_instance
self.outer_instance.somemethod()
def inner_method(self):
self.outer_instance.anothermethod()
全部回答
- 1楼网友:一袍清酒付
- 2021-02-19 13:44
>>> class myclass:
def __init__(self):
print "initialize..."
def __play(self):
print "play..."
>>> a = myclass()
initialize...
>>> a._myclass__play()
play...
>>> dir(a)
['_myclass__play', '__doc__', '__init__', '__module__']python的私有是伪的,还是可以访问到。 方法就是_类名__方法名,python不过是在以__开头的方法名面前加了小动作导致访问不到看起来像私有方法。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯