Path: csiph.com!eternal-september.org!feeder.eternal-september.org!border1.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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'received:134': 0.05; 'subject:Question': 0.05; 'attribute.': 0.09; 'expected.': 0.09; 'fetch': 0.09; 'instance.': 0.09; 'name):': 0.09; 'python': 0.10; 'def': 0.13; 'created.': 0.16; 'received:ac.be': 0.16; 'subject:class': 0.16; 'subject:variable': 0.16; 'variable.': 0.16; 'attribute': 0.18; 'variable': 0.18; 'versions': 0.20; 'assign': 0.22; 'variables.': 0.22; 'code.': 0.23; 'header:In- Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'finds': 0.29; 'class.': 0.30; 'received:be': 0.30; "can't": 0.32; 'skip:_ 10': 0.32; 'run': 0.33; 'class': 0.33; 'problem': 0.33; 'instance': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'method': 0.37; 'things': 0.38; 'self': 0.38; 'test': 0.39; 'to:addr:python.org': 0.40; 'here.': 0.62; 'different': 0.63; 'results': 0.66; 'wish': 0.71; 'hand': 0.82; 'pardon': 0.84; 'schreef': 0.84; 'try,': 0.84; 'hand,': 0.97 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AjgFAEtqClaGuA9G/2dsb2JhbABdhDwBJIMqwjgCghwBAQEBAQGFMAEBAwEjVQYLCxoCBRYLAgIJAwIBAgEPNhMGAgKIFQMKCLcBjzkNR4RCAQEIAiCBIoVRhH2CUIJEF4JSgUMBBI0BiHOLI4FwiHWLB4dIY4QDb4kfAQEB Date: Tue, 29 Sep 2015 12:40:09 +0200 From: Antoon Pardon User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Icedove/31.8.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Question re class variable References: <3948d9cd-24b1-4a3b-8ed0-46bb60a8d738@googlegroups.com> In-Reply-To: <3948d9cd-24b1-4a3b-8ed0-46bb60a8d738@googlegroups.com> Content-Type: text/plain; charset=utf-8 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1443523293 news.xs4all.nl 23815 [2001:888:2000:d::a6]:58551 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:97201 Op 29-09-15 om 11:27 schreef plewto@gmail.com: > I have a perplexing problem with Python 3 class variables. I wish to generate an unique ID each time an instance of GameClass is created. There are two versions of the __gen_id method with test run results for each listed below the code. The problem is that in python you can't change a class variable through an instance. The moment you try, you create an instance attribute. > class GameObject: > > # __instance_registry = {"":None} > __instance_counter = 0 > > def __init__(self, name): > self.__name = str(name) > self.__id = self.__gen_id() > > def __gen_id(self): > ty = self.__class__.__name__ > id = '%s_%d' % (ty, self.__instance_counter) > self.__instance_counter += 1 This last line doesn't work as expected. What happens is equivallent to the following. self.__instance_counter = self.__instance_counter + 1 But the self.__instance_counter are two different things here. On the right hand python finds that self has no __instance_counter attribute so it will fetch the value from the class. However on the left hand, python will create an attribute for self and assign the value to it. Python will not rebind the class variable. -- Antoon Pardon