Python:在内部嵌套类中使用来自外部类的 self.variable

大卫 8

我正在尝试将我的功能组织成类。我想在“内部”类中使用“外部”类中的属性 self.file_list。

class Outer:
    """Outer Class"""
    def __init__(self, file_list):
        self.file_list=file_list
        ## instantiating the 'Inner' class
        self.inner = self.Inner()


    ## inner class
    class Inner:
        """First Inner Class"""
        def __init__(self):
            print (Outer.file_list)  <-- this line doesn't work 

            pass
clean =Outer (['cats', 'dogs'])

但是我收到此错误:

<ipython-input-63-0dfab321da74> in __init__(self)
     10         """First Inner Class"""
     11         def __init__(self):
---> 12             print (self.file_list)
     13 
     14             pass

AttributeError: 'Inner' object has no attribute 'file_list'

如何在内部init方法中访问属性 self.file_list 或者在一个类中将函数组合在一起的传统方法是什么?

尼科·格里芬

Outer是类,并且file_list该类的实例属性。您无法访问Outer.file_list,因为它不存在。

创建 的实例时Inner,您需要传递Outer父实例才能访问其成员。

将您的代码更改为:

class Outer:
    """Outer Class"""
    def __init__(self, file_list):
        self.file_list=file_list
        ## instantiating the 'Inner' class
        self.inner = self.Inner(self)


    ## inner class
    class Inner:
        """First Inner Class"""
        def __init__(self, outer_parent):
            print (outer_parent.file_list)

            pass

应该让它工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

子类继承父类 self.variable

来自分类Dev

python:函数内的self.variable

来自分类Dev

在Python的构造函数中使用self._variable和just self.variable有什么区别

来自分类Dev

Python新手:使用eval()为self.variable赋值

来自分类Dev

Python新手:使用eval()为self.variable赋值

来自分类Dev

在内部使用self的queueSelf队列调用方法

来自分类Dev

在内部使用self的queueS方法的weakSelf

来自分类Dev

何时以及为何使用self .__ dict__而不是self.variable

来自分类Dev

python中self.variable名称和classname.variable之间的区别

来自分类Dev

在Python中使用self

来自分类Dev

Swift内部类可以访问外部类的self吗?

来自分类Dev

在内部类中使用外部类实例作为自己?

来自分类Dev

何时在方法中使用self,&self和&mut self?

来自分类Dev

Python类:覆盖“ self”

来自分类Dev

是否可以使用python中__init__的self.variable值初始化方法的参数?

来自分类Dev

在内部类中使用引用模板参数

来自分类Dev

在内部类中使用Lombok的日志记录

来自分类Dev

是否需要使用无用的object.self.self ..或Class.self.self ..?

来自分类Dev

Python类self.variables

来自分类Dev

在协议中使用 self in function

来自分类Dev

在类中使用没有self的变量-Python

来自分类Dev

在Python类中使用self(在Nuke中创建GUI)

来自分类Dev

在WRDS CRSP查询中使用来自外部文件的Python dict值

来自分类Dev

带有#{self.variable}的Jade for循环未固定列表

来自分类Dev

在python @patch装饰器中使用self

来自分类Dev

在 Python 脚本中使用 self 和参数

来自分类Dev

vue: $this=this 为什么在内部函数中使用外部“this”?

来自分类Dev

在Rails中使用类<< self自动加载类

来自分类Dev

在Rails中使用类<< self自动加载类

Related 相关文章

  1. 1

    子类继承父类 self.variable

  2. 2

    python:函数内的self.variable

  3. 3

    在Python的构造函数中使用self._variable和just self.variable有什么区别

  4. 4

    Python新手:使用eval()为self.variable赋值

  5. 5

    Python新手:使用eval()为self.variable赋值

  6. 6

    在内部使用self的queueSelf队列调用方法

  7. 7

    在内部使用self的queueS方法的weakSelf

  8. 8

    何时以及为何使用self .__ dict__而不是self.variable

  9. 9

    python中self.variable名称和classname.variable之间的区别

  10. 10

    在Python中使用self

  11. 11

    Swift内部类可以访问外部类的self吗?

  12. 12

    在内部类中使用外部类实例作为自己?

  13. 13

    何时在方法中使用self,&self和&mut self?

  14. 14

    Python类:覆盖“ self”

  15. 15

    是否可以使用python中__init__的self.variable值初始化方法的参数?

  16. 16

    在内部类中使用引用模板参数

  17. 17

    在内部类中使用Lombok的日志记录

  18. 18

    是否需要使用无用的object.self.self ..或Class.self.self ..?

  19. 19

    Python类self.variables

  20. 20

    在协议中使用 self in function

  21. 21

    在类中使用没有self的变量-Python

  22. 22

    在Python类中使用self(在Nuke中创建GUI)

  23. 23

    在WRDS CRSP查询中使用来自外部文件的Python dict值

  24. 24

    带有#{self.variable}的Jade for循环未固定列表

  25. 25

    在python @patch装饰器中使用self

  26. 26

    在 Python 脚本中使用 self 和参数

  27. 27

    vue: $this=this 为什么在内部函数中使用外部“this”?

  28. 28

    在Rails中使用类<< self自动加载类

  29. 29

    在Rails中使用类<< self自动加载类

热门标签

归档