Exception AttributeError: "'NoneType' object has no attribute

原创 2013年12月05日 16:07:21

 

今天根据 <简明 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

 

 

相关文章推荐

Flask-JWT 小坑:No JSON object could be decoded;AttributeError: 'NoneType' object has no attribute 'get

使用官方的 “QuickStart” 时, Flask-JWT — Flask-JWT 0.3.2 documentation https://pythonhosted.org/Flask-JWT/ ...

selenium 2.0 'NoneType' object has no attribute 'send_keys'

先把出错的代码贴出来all_handles=action.window_handles for handle in all_handles: if handle!=orginal_page: ...
  • ulebo
  • ulebo
  • 2016年08月18日 18:59
  • 481

Python 【精】AttributeError: 'Module' object has no attribute 'STARTF_USESHOWINDOW'

夫学须志也,才须学也,非学无以广才,非志无以成学。——诸葛亮 生活有度,自得慈铭 ——杜锦阳 今天新来的小弟安装环境遇到个莫名其妙的问题:AttributeError: 'Module'...
  • DJY1992
  • DJY1992
  • 2017年04月06日 11:01
  • 9547

import json后,报错:AttributeError: 'module' object has no attribute 'dumps,原因分析及解决方法

编写python代码,import json后,报错:AttributeError: 'module' object has no attribute 'dumps',如下图所示: 报错信息...

RabbitVCS(0.14.2)不显示右键设置(settings)菜单,AttributeError: 'gtk.VBox' object has no attribute 'set_visible

RabbitVCS是Linux的svn客户端,对于在windows下用惯了TortoiseSVN的人来说,在linux下使用纯命令行的svn命令实在是太折磨人了,RabbitVCS就是一个类似于Tor...
  • 10km
  • 10km
  • 2016年09月29日 12:02
  • 849

Python AttributeError: 'module' object has no attribute xxxx

原因:   导致在这个问题的原因是你的模块名和你要引用的类或方法或变量的名字重了。说通俗点就是,python脚本名字(模块名)和你要引用的内容(模块内的类,变量等)的名字重复了,导致你原本“类->属性...

Python下Pip安装包下的AttributeError: 'Requirement' object has no attribute 'project_name'

引言: 在基于pip安装第三方依赖库的过程中,会碰到AttributeError: 'Requirement' object has no attribute 'project_name'的错误信息,...

Python问题:AttributeError: '_csv.reader' object has no attribute 'next' 以及UnicodeDecodeError: 'gbk' co

先看一下代码: 报错: 解决方式: 这是我在网上找到的解决方式,先说一下出现问题的原因:出现AttributeError主要是因为python 2和 3 的版...

Django: AttributeError: 'str' object has no attribute 'resolve'

转载自:http://redsymbol.net/articles/django-attributeerror-str-object-no-attribute-resolve/ He...

TensorFlow1.0 运行报错 AttributeError: type object 'NewBase' has no attribute 'is_abstract'

安装TF后,import tensorflow报错: I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Exception AttributeError: &quot;'NoneType' object has no attribute
举报原因:
原因补充:

(最多只允许输入30个字)