Python - 析构修改全局变量异常(__del__ of <__main__)
来源:互联网 发布:玩dnf老是网络中断 编辑:程序博客网 时间:2024/05/29 08:34
析构(__del__)修改全局变量异常(__del__ of <__main__)
本文地址: http://blog.csdn.net/caroline_wendy/article/details/23377053
异常描述: "Exception AttributeError: "'NoneType' object has no attribute 'number'" in <bound method Girl.__del__ of <__main__.Girl object at 0x01D60ED0>> ignored"
出现在析构(__del__)中, 操作类的变量, 有可能, 导致在析构类之后, 类的变量不存在, 造成异常;
注意Python文档中,__del__的警告信息, 可以查询;
也可参见: http://bytes.com/topic/python/answers/41024-python-bug-processing-__del__-method
解决方法, 析构时, 操作本类的全局变量(self.__class__).
代码:
# -*- coding: utf-8 -*-#eclipse pydev, python 3.3#by C.L.Wangclass Girl: _number = 0 def __init__(self, name): self.name = name Girl._number += 1 def __del__(self): Girl._number -= 1 #self.__class__.number -= 1 def sayName(self): print(self.name) @staticmethod def howMany(): '''静态方法''' print("I know {0:d} girls. ".format(Girl._number)) Girl.howMany()wang = Girl('Wang')Girl.howMany()liu = Girl('Liu')Girl.howMany()print(list(globals()))
输出:
I know 0 girls. I know 1 girls. I know 2 girls. ['Girl', '__builtins__', '__name__', '__loader__', '__package__', '__cached__', '__doc__', '__file__', 'liu', 'wang']Exception AttributeError: "'NoneType' object has no attribute '_number'" in <bound method Girl.__del__ of <__main__.Girl object at 0x01D82CF0>> ignoredException AttributeError: "'NoneType' object has no attribute '_number'" in <bound method Girl.__del__ of <__main__.Girl object at 0x01D70ED0>> ignored
因为'Girl'全局类, 出现在实例之前, 即'wang', 'liu', 所以出现警告, 修改:
def __del__(self): #Girl._number -= 1 self.__class__._number -= 1
即可.
1 0
- Python - 析构修改全局变量异常(__del__ of <__main__)
- Python Class __init__ __del__ 构造,析构过程解析
- python __init__构造函数 和 __del__析构函数
- Python Class __init__ __del__ 构造,析构过程解析
- Python Class __init__ __del__ 构造,析构过程解析
- Python __del__
- python __del__ 调用时机
- Python的__del__()方法
- 21 Python __del__
- python中__del__使用方法
- Python----__del__, 生命周期结束
- python中的__del__方法
- Python __init__和__del__方法
- Python中__del__如何使用
- 「python」__del__()方法
- python中怎样修改全局变量
- python 全局变量引用与修改
- python 中全局变量的修改
- 第一个OC的类
- 【开发平台】VM10.0非全屏情况下去掉黑边
- C++学习日记之动态内存分配构建stack类
- java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
- 欧几里得算法之辗转相除法
- Python - 析构修改全局变量异常(__del__ of <__main__)
- ubuntu 各个版本都无法安装的解决,同时有集成显卡和独立显卡的机器
- 经典内部排序之选择排序
- 基于TILE-GX实现快速数据包处理框架-netlib实现分析
- Matlab的imfilter函数用法
- cell全部选择
- VB6 更改ComboBox的Style属性(对应C# Dropdown属性)
- 成员函数、友元函数和一般函数有区别
- JAVA键盘读入方法