Path: csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!border2.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'dependency': 0.07; 'run,': 0.07; "subject:' ": 0.07; "'%s": 0.09; 'cleaned': 0.09; 'name):': 0.09; 'python': 0.10; 'def': 0.13; '3.2.': 0.16; '__del__': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'subject:object': 0.16; 'wrote:': 0.16; 'all,': 0.20; 'occurs': 0.22; 'ok.': 0.22; 'code,': 0.23; 'code.': 0.23; 'finished': 0.23; 'header:In-Reply-To:1': 0.24; 'testing': 0.25; 'header:User- Agent:1': 0.26; "doesn't": 0.26; 'error': 0.27; 'skip:# 10': 0.27; "skip:' 10": 0.28; 'looks': 0.29; 'behaviour': 0.29; 'print': 0.30; 'fixed': 0.31; 'skip:_ 10': 0.32; 'up.': 0.32; 'says': 0.32; 'class': 0.33; 'problem': 0.33; 'recommended': 0.34; 'replace': 0.35; 'something': 0.35; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'skip:z 10': 0.38; 'version': 0.38; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'some': 0.40; 'skip:u 10': 0.61; 'strange': 0.63; 'subject:skip:A 10': 0.63; 'latest': 0.64; 'here': 0.66; 'population': 0.72; "'we": 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=CvRCCSMD c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=EBOSESyhAAAA:8 a=Yo2XpZhZnQAA:10 a=IkcTkHD0fZMA:10 a=HMRNdTE64RfXYlkTt50A:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 Subject: Re: Exception AttributeError: "'NoneType' object has no attribute 'population'" To: python-list@python.org References: From: MRAB Date: Mon, 24 Aug 2015 12:50:05 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1440417014 news.xs4all.nl 23846 [2001:888:2000:d::a6]:34618 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95610 On 2015-08-24 06:49, 344276105 wrote: > Hi all, > I am a python learner. I encountered a problem when i was testing the > following code. What is strange is that if I replace the object name > with zhang, the program would be ok. And if I replace the > Person.population with self.__class__.population, it will also be ok. So > what is the matter? > Here is the code, > > #!/usr/bin/python > # Filename: objvar.py > > class Person: > population = 0 > > def __init__(self, name): > self.name = name > print '(Initializing %s...)' % self.name > > Person.population += 1 > > def __del__(self): > 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): > print 'Hi, my name is %s.' % self.name > > def howMany(self): > if Person.population == 1: > print 'I am the only person here.' > else: > print 'We have %d persons here.' % Person.population > > > zhangsan = Person('Swaroop') > zhangsan.sayHi() > zhangsan.howMany() > > lisi = Person('LiSi') > lisi.sayHi() > lisi.howMany() > > zhangsan.sayHi() > zhangsan.howMany() > The error occurs because the program has finished and Python is cleaning up. Unfortunately, it looks like when the __del__ methods run, the Person class has already been cleaned up (Person is None at that point). It looks like that behaviour was fixed in Python 3.2. By the way, it's recommended that you use the latest version of Python 3 unless you require Python 2 because of some dependency on something that doesn't (yet?) work on Python 3.