今天根据 <简明 python 教程http://sebug.net/paper/python/ch11s06.html>学习案例时,遇到 下面的异常.
#!/usr/local/python # -*- coding:utf-8 -*- # filename: objvar.py class Person: ''' Represents a person''' population = 0 def __init__(self,name): ''' Initialize the person's data. This is just new a object . ''' self.name = name print "(Initializing %s)" % self.name Person.population +=1 def __del__(self): ''' I am dying. This is a del method''' print "%s says bye." % self.name Person.population -=1 if Person.population == 0: print 'I am the last one' else: print "There are still %d people left." % Person.population def sayHi(self): ''' Greeting by the person This is a sayHi method''' print "Hi ,my name is %s" % self.name def howMany(self): ''' Prints the current person This is a howMany method.''' if Person.population == 1: print 'I am the only person here.' else: print 'We have %d persons here.' % Person.population if(__name__ == '__main__'): person1 = Person("ming") person1.sayHi() person1.howMany() person2 = Person("Jack") person2.sayHi() person2.howMany() person1.sayHi() person1.howMany()
(Initializing ming) Hi ,my name is ming I am the only person here. (Initializing Jack) Hi ,my name is Jack We have 2 persons here. Hi ,my name is ming We have 2 persons here. Jack says bye. Exception AttributeError: "class Person has no attribute '__class__'" in <bound method Person.__del__ of <__main__.Person instance at 0xb723cfec>> ignored luming says bye. Exception AttributeError: "type object 'NoneType' has no attribute 'population'" in <bound method Person.__del__ of <__main__.Person instance at 0xb723cfac>> ignored
原因如下:
At interpreter shutdown, the module's global variables are set to None before the module itself is released.
__del__ methods may be called in those precaries circumstances, and should not rely on any global state.
将__del__方法中对类变量的访问方式改为如下即可:
def __del__(self):
self.__class__.population -= 1
更改后方法 __del__变为:
def __del__(self): ''' I am dying. This is a del method''' print "%s says bye." % self.name self.__class__.population -=1 if self.__class__.population == 0: print 'I am the last one' else: print "There are still %d people left." % self.__class__.population
相关推荐
在Python编程中,特别是在进行网页抓取(网络爬虫)时,可能会遇到`AttributeError: 'NoneType' object has no attribute 'children'`这样的错误。这个错误表明在尝试访问一个对象的`children`属性时,该对象实际上...
python3 server.py 127.0.0.1 8888 ...AttributeError: module ‘os’ has no attribute ‘exit’ 部分代码入下: from socket import * import sys,os #实现登录 def do_login(s,user,name,addr): for i in user: i
【多线程爬虫出现报错AttributeError: ‘NoneType’ object has no attribute ‘xpath’】 一、前言 在学习Python网络爬虫的过程中,多线程爬虫是一种提高效率的有效方式,它允许同时处理多个任务,从而缩短整体...
NULL 博文链接:https://wuhuizhong.iteye.com/blog/2228085
AttributeError: module 'tensorflow.compat.v1' has no attribute 'contrib'的问题您具体怎么解决问题具体解决的seq_loss.py文件
在本篇内容中,我们将深入探讨在Python3环境下遇到`AttributeError: 'dict' object has no attribute 'iteritems'`这一错误的原因、分析及解决方案。 1. **Python 2 与 Python 3 的差异** Python 2 和 Python 3 是...
如图示,会出现如AttributeError: ‘DataParallel’ object has no attribute ‘xxx’的错误。 原因:在使用net = torch.nn.DataParallel(net)之后,原来的net会被封装为新的net的module属性里。 解决方案:所有在...
成功解决AttributeError: 'str' object has no attribute 'to'
资源分类:Python库 所属语言:Python 资源全名:python-didl-lite-1.1.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
AttributeError: 'NoneType' object has no attribute 'encoding 使用pyCharm+python3+pyMysql+mysql5.56 数据库连接: connect = pymysql.Connect(host='localhost',port=3333,user='root',passwd='root',db='...
langchain-chatchat在window上使用cpu运行Qwen-1_8B-Chat时遇到ERROR: object of type ‘NoneType‘ has no len(),这个问题其实是因为"addmm_impl_cpu_" not implemented for 'Half’这个根本原因导致的,也就是cpu...
调试递归神经网络(RNN)的时候出现如下错误: AttributeError: module 'tensorflow.contrib.rnn' has no attribute 'core_rnn_cell' 顺便问一句,资源分怎么设置免费啊
问题描述及解决: 原创文章 1获赞 1访问量 17 关注 私信 展开阅读全文 作者:Branson233
import pymysql #创建连接 con = pymysql.connect(host='localhost',user='root',password='123456',port=3306,database='zhy') #创建游标对象 cur = con.curson() #编写查询的sql语句 ...except Exception as
Python的字符集处理实在蛋疼,目前使用UTF-8居多,然后默认使用的字符集是ascii,所以我们需要改成utf-8 查看目前系统字符集 复制代码 代码如下: import sys print sys.getdefaultencoding() 执行: ...
File "D:\anaconda\lib\site-packages\django\core\handlers\exception.py", line 34, in inner response = get_response(request) File "D:\anaconda\lib\site-packages\django\core\handlers\base.py", line ...
今天安装pymysql时发生了错误AttributeError: module ‘pip’ has no attribute ‘main’ 解决方法如下: 1.找到PyCharm 2017.1\helpers\packaging_tool.py 2.打开packaging_tool.py,注意,最好用pycharm打开,因为...
在学习过程中,可能会遇到错误“AttributeError: ‘PyQt5.QtCore.pyqtSignal’ object has no attribute ‘connect’”,这通常是由于不正确地定义或使用自定义信号导致的。本文将详细解释这个问题以及如何解决。 ...