昨晚在整理自己的python脚本的时候,想把其中一个脚本中的print
函数全都改成logging
包中的相关函数。改完后一运行却出现了Exception AttributeError: 'NoneType' object has no attribute
的错误,网上搜了一下没找到相关答案。上午再想了想,原因应该是跟python对象的析构有关,具体分析过程如下:
由于原脚本业务部分过长,这里把关键的几个部位抽出来做了个演示程序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | # -*- coding: UTF-8 -*- # File: destrution_attribute_error_nonetype1.py # Description: python自动析构时出现Exception AttributeError: 'NoneType' object has no attribute问题的示例程序 # (c) 2018.12.19 vfhky https://typecodes.com/python/destrution_attribute_error_nonetype1.html import threading import logging # MYSQL 的简单封装 class CMySQL: # 线程锁 _instance_lock = threading.Lock() # 数据库连接对象 __db = None # 游标对象 __cursor = None def __init__(self, *args, **kwargs): pass def __new__(cls, *args, **kwargs): pass # 析构函数,释放对象时使用 def __del__(self): # 关闭数据库连接 if self.__db: self.__db.close() # 自动析构时这里会出问题:'NoneType' object logging.info("-------> close db.") else: # 自动析构时这里也会出问题:'NoneType' object logging.warning("-------> db unconnected or had been closed.") if __name__ == "__main__": # 创建实例 db_obj = CMySQL() # 输出到控制台 logging.basicConfig(level=logging.INFO, format='[%(asctime)s][L:%(lineno)d][%(levelname)s][%(process)d] %(message)s', datefmt='%d %b %Y %H:%M:%S') logging.info("================================== END ==================================") |
---|
执行上面的程序,在Linux终端上就会出现Exception AttributeError: 'NoneType' object has no attribute 'warning'" in <bound method CMySQL.__del__ of <__main__.CMySQL instance at 0x7f4b3f9b7128>>
的错误。
如下图所示:
其实是不了解python的析构过程导致的:当main函数结束后(输出图中的END
字样),意味着进程即将退出,那么会自动调用对象的析构函数进行析构,这点Python和C++是一样的。
由于logging
模块中的类对象(包括成员变量、成员函数等)已经被析构了,所以当执行CMySQL
对象的析构函数__del__
中的logging.warning
函数时会出现"'NoneType' object has no attribute 'warning'
的错误。
解决方法很简单,只要增加一个封装MySQL链接关闭的函数close
就行了,当main函数结果调用即可。下面的代码是针对这个问题的改进版本。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | # -*- coding: UTF-8 -*- # File: destrution_attribute_error_nonetype1_1.py # Description: 修正Exception AttributeError: 'NoneType' object has no attribute问题的示例程序 # (c) 2018.12.19 vfhky https://typecodes.com/python/destrution_attribute_error_nonetype1.html import threading import logging # MYSQL 的简单封装 class CMySQL: # 线程锁 _instance_lock = threading.Lock() # 数据库连接对象 __db = None # 游标对象 __cursor = None def __init__(self, *args, **kwargs): pass def __new__(cls, *args, **kwargs): pass # 关闭数据库连接 def close(self): if self.__db: self.__db.close() self.__db = None self.__cursor = None logging.info("-------> close db.") else: logging.warning("-------> db unconnected or had been closed.") # 析构函数,释放对象时使用 def __del__(self): # 关闭数据库连接 if self.__db: self.__db.close() self.__db = None self.__cursor = None # logging.info("-------> close db.") else: pass #logging.warning("-------> db unconnected or had been closed.") if __name__ == "__main__": # db_obj = CMySQL() # 输出到控制台 logging.basicConfig(level=logging.INFO, format='[%(asctime)s][L:%(lineno)d][%(levelname)s][%(process)d] %(message)s', datefmt='%d %b %Y %H:%M:%S') logging.info("================================== END ==================================") # 关闭数据库连接 db_obj.close() |
---|
运行效果如下图所示: