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

 

 

python文件运行报错: 'NoneType' object has no attribute 'TF_DeleteStatus'

编译python文件时,报错:’NoneType’ object has no attribute ‘TF_DeleteStatus’的处理办法...
  • u010187278
  • u010187278
  • 2017年09月08日 15:57
  • 1293

AttributeError: 'NoneType' object has no attribute

python 读取excel文件报错:  basetable = data.sheet_by_index(0) AttributeError: 'NoneType' object has no a...
  • page_qiu
  • page_qiu
  • 2016年03月03日 09:04
  • 1418

Python报错之:AttributeError: 'NoneType' object has no attribute 'seq'

在用matplotlib进行数据可视化、给图形添加标签数据标签时遇到一个报错:AttributeError: 'NoneType' object has no attribute 'seq'. 记录...
  • zmisno1
  • zmisno1
  • 2018年01月21日 10:28
  • 173

AttributeError: 'NoneType' object has no attribute 'execute' 错误的解决

运行Python连接mysql数据,查询表格中的数据时报了如下错误AttributeError: 'NoneType' object has no attribute 'execute' 的解决方案...
  • helloxiaozhe
  • helloxiaozhe
  • 2017年07月15日 11:31
  • 915

python爬虫时 AttributeError: 'NoneType' object has no attribute 'children'错误提示

这段时间突然对网络爬虫比较感兴趣,于是入手了一下。今天看到一个大学排名的网站,想将网页上的排名信息爬取下来。代码如下: for tr in soup.find('tbody').childr...
  • qq_38329811
  • qq_38329811
  • 2017年08月05日 21:22
  • 3031

使用Buildozer部署时,出现AttributeError: 'NoneType' object has no attribute 'group'错误的解决

在Ubuntu12.04上使用Buildozer构建app时,出现了AttributeError: ‘NoneType’ object has no attribute ‘group’错误,如下图所示...
  • fangxuejiang
  • fangxuejiang
  • 2015年10月25日 19:47
  • 2231

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
  • 780

scapy AttributeError: 'NoneType' object has no attribute 'route'解决办法

我在用scapy构造畸形icmp报文的时候遇到这么一个问题 Traceback (most recent call last):   File "icmp_abnormal.py", line 1...
  • ligengyong2010
  • ligengyong2010
  • 2015年03月24日 11:51
  • 1544

AttributeError: 'NoneType' object has no attribute 'sc' 解决方法(二)

上一次本以为可以解决了这个问题,然而并没有那么地简单。博主最近在edx网站学习pyspark,想打一下视频上的代码,结果报错了,依旧是报了“AttributeError:’NoneType’ obje...
  • ZengHaihong
  • ZengHaihong
  • 2016年12月01日 16:48
  • 7319

如何解决CRITICAL glance [-] AttributeError: 'NoneType' object has no attribute 'drivername'

今天在配置OpenStack的Glance时,前边进行的都很顺利,当作到这一步时sudo glance-manage db_sync时出现了如下错误 根据错误提示,想到可能是配置问题,于是就查找...
  • xingjiarong
  • xingjiarong
  • 2015年07月12日 11:10
  • 3339
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Exception AttributeError: &quot;'NoneType' object has no attribute
举报原因:
原因补充:

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