今天根据 <简明 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
相关推荐
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
NULL 博文链接:https://wuhuizhong.iteye.com/blog/2228085
多线程爬虫出现报错AttributeError: ‘NoneType’ object has no attribute ‘xpath’一、前言二、问题三、思考和解决问题四、运行效果 一、前言 mark一下,本技术小白的第一篇CSDN博客! 最近在捣鼓爬虫,看的是...
AttributeError: module 'tensorflow.compat.v1' has no attribute 'contrib'的问题您具体怎么解决问题具体解决的seq_loss.py文件
补充知识:Pandas使用DataFrame出现错误:AttributeError: ‘list’ object has no attribute ‘astype’ 在使用Pandas的DataFrame时出现了错误:AttributeError: ‘list’ object has no attribute ‘astype’ 代码...
成功解决AttributeError: 'str' object has no attribute 'to'
Pycharm关于AttributeError: ‘DataFrame’ object has no attribute ‘score’的错误 import pandas data = pandas.read_excel( r"C:\Users\ASUS\Desktop\0012\data7.1.2.xlsx", ) data.score.describe() # 逐项...
在运行嵩天老师python爬虫课中单元6中的实例“中国大学排名爬虫”会出现如下图错误:AttributeError: ‘NoneType’ object has no attribute ‘children’ 意思是 ‘NoneType’ 对象没有属性 ‘children’ ,这个...
主要跟大家介绍了关于在Python3下错误AttributeError: 'dict' object has no attribute 'iteritems'的分析与解决方法,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
如图示,会出现如AttributeError: ‘DataParallel’ object has no attribute ‘xxx’的错误。 原因:在使用net = torch.nn.DataParallel(net)之后,原来的net会被封装为新的net的module属性里。 解决方案:所有在...
资源分类:Python库 所属语言:Python 资源全名:python-didl-lite-1.1.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
调试递归神经网络(RNN)的时候出现如下错误: AttributeError: module 'tensorflow.contrib.rnn' has no attribute 'core_rnn_cell' 顺便问一句,资源分怎么设置免费啊
import pymysql #创建连接 con = pymysql.connect(host='localhost',user='root',password='123456',port=3306,database='zhy') #创建游标对象 cur = con.curson() #编写查询的sql语句 ...except Exception as
问题描述及解决: 原创文章 1获赞 1访问量 17 关注 私信 展开阅读全文 作者:Branson233
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 ...
AttributeError: 'NoneType' object has no attribute 'encoding 使用pyCharm+python3+pyMysql+mysql5.56 数据库连接: connect = pymysql.Connect(host='localhost',port=3333,user='root',passwd='root',db='...
本机环境: cudatoolkit = 10.1.243 cudnn = 7.6.5 tensorflow-gpu = 2.1.0 keras-gpu = 2.3.1 今天在以TensorFlow2.1.0为后端的Keras中使用TensorBoard时报错,发现原因是keras和tf.keras混用导致的。...
问题解决:AttributeError: module ‘paddle.fluid’ has no attribute ‘EndStepEvent’问题描述解决思路问题解决 问题描述 在使用paddle.fluid导入EndStepEvent过程中 global step if isinstance(event, fluid....
今天安装pymysql时发生了错误AttributeError: module ‘pip’ has no attribute ‘main’ 解决方法如下: 1.找到PyCharm 2017.1\helpers\packaging_tool.py 2.打开packaging_tool.py,注意,最好用pycharm打开,因为...
AttributeError: ‘NoneType’ object has no attribute ‘something’ something可以用实际属性的名称替换 当我们认为我们正在使用特定类或对象的实例时会看到这些错误,实际上是Python内置类型’None‘,’None‘ ...