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


Groups > comp.lang.python > #97208

Re: Question re class variable

From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: Question re class variable
Date 2015-09-29 08:21 -0400
Organization IISS Elusive Unicorn
References <3948d9cd-24b1-4a3b-8ed0-46bb60a8d738@googlegroups.com> <mailman.232.1443523293.28679.python-list@python.org> <vg3k2r9wk34.fsf@coffee.modeemi.fi>
Newsgroups comp.lang.python
Message-ID <mailman.237.1443529284.28679.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, 29 Sep 2015 14:17:35 +0300, Anssi Saari <as@sci.fi> declaimed the
following:

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

	They get an incremental ID because he constantly searches the
class-wide "instance registry" for the duplicate values -- which are always
being generated since the counter starts at 0 (+1) for each instance...
Would get quite slow if enough instances were to be created... While the
registry was initialized with

    # __instance_registry = {"":None}

Preloaded with a dummy value just so the first pass of the "while" loop
wouldn't drop out.

	Whereas the counter is a rebinding into a local, the registry is a
mutation of the classwide value, not a rebinding -- so it IS shared among
all.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

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