Python python __def__ Exception AttributeError: "'NoneType' object has no attribute

class Person:
    ‘‘‘Represents a person.‘‘‘
    population = 0

    def __init__(self,name):
        ‘‘‘Initializes the person‘s data.‘‘‘
        self.name = name
        print ‘(Initializing %s)‘ % self.name

        Person.population +=1

    def __del__(self):
        ‘‘‘I am dying.‘‘‘
        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.

        Really, that‘s all it does.‘‘‘
        print ‘Hi, my name is %s.‘ % self.name

    def howMany(self):
        ‘‘‘Prints the current population.‘‘‘
        if Person.population == 1:
            print ‘I am the only person here.‘
        else:
            print ‘We have %d persons here.‘ % Person.population

jerry = Person(‘Jerry‘)
jerry.sayHi()
jerry.howMany()

qiu = Person(‘Qiu‘)
qiu.sayHi()
qiu.howMany()

jerry.sayHi()
jerry.howMany()

出现如下错误:

Exception AttributeError: "‘NoneType‘ object has no attribute ‘population‘" in <bound method Person.__del__ of <__main__.Person instance at 0x01AF97D8>> 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

  

Python python __def__ Exception AttributeError: "'NoneType' object has no attribute

时间: 2024-10-02 10:27:36

Python python __def__ Exception AttributeError: "'NoneType' object has no attribute的相关文章

python提示AttributeError: &#39;NoneType&#39; object has no attribute &#39;append&#39;

在写python脚本时遇到AttributeError: 'NoneType' object has no attribute 'append' a=[] b=[1,2,3,4] a = a.append(b) 执行一次后发现a的类型变为了NoneType. 下次执行时就会出现如题所示的错误. 把a = a.append(b)改为a.append(b)后问题解决. 原因:append会修改a本身,并且返回None.不能把返回值再赋值给a. python提示AttributeError: 'Non

python提示AttributeError: &#39;NoneType&#39; object has no attribute &#39;append&#39;【转发】

在写python脚本时遇到AttributeError: 'NoneType' object has no attribute 'append' a=[] b=[1,2,3,4] a = a.append(b) 执行一次后发现a的类型变为了NoneType. 下次执行时就会出现如题所示的错误. 把a = a.append(b)改为a.append(b)后问题解决. 原因:append会修改a本身,并且返回None.不能把返回值再赋值给a.--------------------- 作者:冰雪凌萱

Python问题——AttributeError: &#39;NoneType&#39; object has no attribute &#39;append&#39;

python提示AttributeError: 'NoneType' object has no attribute 'append' Python问题——AttributeError: 'NoneType' object has no attribute 'append' f=open("data.csv")for line in f: line = line.strip("\n") ls = line.split(",") lt=[] for

python-pip升级报错- AttributeError: &#39;NoneType&#39; object has no attribute &#39;bytes&#39;

正常的pip升级命令: python -m pip install --upgrade pip 在pytharm里面创建了一个Python项目,pytharm会自动搭建一个新的Python环境,在当前的目录下使用 python -m pip install --upgrade pip 会报错 AttributeError: 'NoneType' object has no attribute 'bytes' 可以使用如下方式 easy_install -U pip python-pip升级报错-

python3 AttributeError: &#39;NoneType&#39; object has no attribute &#39;split&#39;

1 from wsgiref.simple_server import make_server 2 3 def RunServer(environ, start_response): 4 start_response('200 ok',[('Content-Type','text/html')]) 5 return '<h1>Hello world</h1>' 6 7 if __name__ == '__main__': 8 httpd = make_server('127.0.0

如何解决CRITICAL glance [-] AttributeError: &#39;NoneType&#39; object has no attribute &#39;drivername&#39;

今天在配置OpenStack的Glance时,前边进行的都很顺利,当作到这一步时sudo glance-manage db_sync时出现了如下错误 根据错误提示,想到可能是配置问题,于是就查找了配置文档,发现需要在/etc/glance/glance-registry.conf和/etc/glance/glance-api.conf中加入下面一句话 sql_connection = mysql://glance:[email protected]/glance 格式如下:sql_connect

关于AttributeError: &#39;NoneType&#39; object has no attribute &#39;send_keys&#39;

在学web自动化测试时,通过PO模型将特定页面的一些元素及元素操作放在特定页面模块中, 然后提取公共的部分, 如元素等待WebDriverWait, 元素操作send_keys, click, 获取元素文本信息, 获取属性值等, 放在公共的页面模块里, 即base_page.py, 但在实现过程中我的代码报了错: 想了很久都没发现错在哪里, 那种感觉真不好受... 昨夜西风凋碧树.. 衣带渐宽终不悔... 众里寻他千百度.... 咦, 那个错误不就在眼皮底下么? 原来是少了一个 return .

AttributeError: &#39;NoneType&#39; object has no attribute &#39;find&#39;

遇到这个问题是因为读取excel表格的数据时默认是str类型,但是excel有空行时,类型读取为NoneType,如果把类型转换为str或者把excel里的空行删掉就不会有这个问题 AttributeError: 'NoneType' object has no attribute 'find' 原文地址:https://www.cnblogs.com/wangguniang/p/12101189.html

Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法

最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attribute 'xxx'".这其实是.pyc文件存在问题. 问题定位: 查看import库的源文件,发现源文件存在且没有错误,同时存在源文件的.pyc文件 问题解决方法: 1. 命名py脚本时,不要与python预留字,模块名等相同 2. 删除该库的.pyc文件(因为py脚本每次运行时均会生成.pyc文件