昨晚在整理自己的 python 脚本的时候,想把其中一个脚本中的 print 函数全都改成 logging 包中的相关函数。改完后一运行却出现了 Exception AttributeError: 'NoneType' object has no attribute 的错误,网上搜了一下没找到相关答案。上午再想了想,原因应该是跟python对象的析构有关,具体分析过程如下:
1 示例程序
由于原脚本业务部分过长,这里把关键的几个部位抽出来做了个演示程序。
# -*- 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):
p