Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #97206

Re: Question re class variable

Path csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <antoon.pardon@rece.vub.ac.be>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.011
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'received:134': 0.05; 'subject:Question': 0.05; 'attribute.': 0.09; 'instance.': 0.09; 'python': 0.10; 'def': 0.13; 'created.': 0.16; 'increment': 0.16; 'received:ac.be': 0.16; 'subject:class': 0.16; 'subject:variable': 0.16; 'variable': 0.18; '>>>': 0.20; 'versions': 0.20; 'variables.': 0.22; 'code.': 0.23; 'this:': 0.23; 'thus': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'skip:_ 20': 0.26; 'function': 0.28; 'received:be': 0.30; "can't": 0.32; 'skip:_ 10': 0.32; 'run': 0.33; 'class': 0.33; 'problem': 0.33; 'instances': 0.33; 'clear': 0.35; 'instance': 0.35; "isn't": 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'method': 0.37; 'version': 0.38; 'self': 0.38; 'why': 0.39; 'test': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'results': 0.66; 'wish': 0.71; 'pardon': 0.84; 'schreef': 0.84; 'try,': 0.84
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result AjgFAAx9ClaGuA9G/2dsb2JhbABdhDwBJIMqwjoCgh0BAQEBAQGFMAEBBCNVEQsaAgUWCwICCQMCAQIBDzYTBgICiBUDErcCjzgNR4RCAQEIAiCBIoVRhH2CUIJEF4JSgUMBBJV0iyOBcIFPhyaLB4NZg29jhANvAYkeAQEB
Date Tue, 29 Sep 2015 14:02:34 +0200
From Antoon Pardon <antoon.pardon@rece.vub.ac.be>
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> <mailman.232.1443523293.28679.python-list@python.org> <vg3k2r9wk34.fsf@coffee.modeemi.fi>
In-Reply-To <vg3k2r9wk34.fsf@coffee.modeemi.fi>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.236.1443528161.28679.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1443528161 news.xs4all.nl 23780 [2001:888:2000:d::a6]:55234
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:97206

Show key headers only | View raw


Op 29-09-15 om 13:17 schreef Anssi Saari:
> Antoon Pardon <antoon.pardon@rece.vub.ac.be> writes:
>
>> 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.
> That much is clear but why does his other version of __gen_id() work
> (after a fashion)? It doesn't increment the class variable but the
> instances get an incremental id.
>
> The function was like this:
>
>     def __gen_id(self):
>         ty = self.__class__.__name__
>         id = ''
>         while id in self.__instance_registry:
>             id = '%s_%d' % (ty, self.__instance_counter)
>             self.__instance_counter += 1
>         self.__instance_registry[id] = self
>         return id

Because you check against the class variable __instance_registry. That variable
isn't rebound, it is mutated, so it remains a class variable and can thus be used
to check which id's are already in use. So you increment your counter until the
corresponding id is not in the __instance_registry.

-- 
Antoon Pardon. 

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Question re class variable plewto@gmail.com - 2015-09-29 02:27 -0700
  Re: Question re class variable alister <alister.nospam.ware@ntlworld.com> - 2015-09-29 10:00 +0000
    Re: Question re class variable John Gordon <gordon@panix.com> - 2015-09-29 14:44 +0000
      Re: Question re class variable Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-09-29 20:41 -0400
  Re: Question re class variable Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-09-29 12:40 +0200
    Re: Question re class variable Anssi Saari <as@sci.fi> - 2015-09-29 14:17 +0300
      Re: Question re class variable Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-09-29 14:02 +0200
      Re: Question re class variable Steven D'Aprano <steve@pearwood.info> - 2015-09-29 22:06 +1000
      Re: Question re class variable Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-09-29 08:21 -0400
  Re: Question re class variable jmp <jeanmichel@sequans.com> - 2015-09-29 13:02 +0200
  Re: Question re class variable jmp <jeanmichel@sequans.com> - 2015-09-29 13:11 +0200

csiph-web