
python 3.12 中 init 中的属性不可用
在 python 3.12 中编写了一个程序,但是运行时遇到了一个错误,提示“attributeerror:getconfig 对象没有属性 conf”。
错误代码如下:
class getconfig(object): def __int__(self): # 创建一个属性 self.conf = configparser.configparser() def get_db_host(self): return self.conf.get("db", "host")if __name__ == "__main__": gc1 = getconfig() var = gc1.get_db_host()
错误消息:
立即学习“Python免费学习笔记(深入)”;
traceback (most recent call last): file "getconfig.py", line 21, in var = gc1.get_db_host() ^^^^^^^^^^^^^^^^^ file "getconfig.py", line 17, in get_db_host return self.conf.get("db", "host") ^^^^^^^^^attributeerror: 'getconfig' object has no attribute 'conf'
为什么会出现此错误?
错误的根本原因在于类构造方法的拼写。在 python 中,类构造方法的名称是 __init__,而不是 __int__。
修正后的代码如下:
class GetConfig(object): def __init__(self): # 创建一个属性 self.conf = configparser.ConfigParser() def get_db_host(self): return self.conf.get("DB", "host")if __name__ == "__main__": gc1 = GetConfig() var = gc1.get_db_host()
以上就是Python 3.12中`__int__`导致属性不可用:为什么我的GetConfig对象没有’conf’属性?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1353895.html
微信扫一扫
支付宝扫一扫