python - 无法访问类属性

标签 python class python-2.7

<分区>

我有一段非常简单的代码,虽然卡在了一个愚蠢但难以弄清楚的情况下。我也是 python 的新手,下面是我的代码

class Function:
    def _init_(self):
        self.Name=""
        self.SubFunctions=[]
        self.IsSingle=False

if __name__ == '__main__':
    test=Function()
    test.Name="testing"
    test.SubFunctions.append("param1") # gives an AttributeError: Function instance has no attribute 'SubFunctions'

如果有人能帮助我解决我的问题。

最佳答案

你拼错了 __init__ ,它在开始和结束处有两个下划线(总共有 4 个下划线)。您的 _init_ 方法永远不会被调用。

关于python - 无法访问类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28500214/

相关文章:

Python Json 解析

java - 在 Groovy 中重新加载类

java - 如何从抽象类实例化子类对象中访问子类方法

java - 为什么我们不能从内部类构造函数访问另一个内部类?

python - TypeError : Can't instantiate abstract class <. ..> 使用抽象方法

python - 如何使用 Tox/Py.test 显示完整的 Python Traceback

python - 使用python从文本文件插入mysql表

python - 基于多维数据在python中绘制类之间的边界线?

python - 从 Robot Framework 中的查询结果中删除尾随逗号

php - Python 中的安全 MySQL 连接