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


Groups > comp.lang.python > #97200

Re: Question re class variable

From alister <alister.nospam.ware@ntlworld.com>
Newsgroups comp.lang.python
Subject Re: Question re class variable
Date 2015-09-29 10:00 +0000
Organization Aioe.org NNTP Server
Message-ID <mudnfg$cqo$1@speranza.aioe.org> (permalink)
References <3948d9cd-24b1-4a3b-8ed0-46bb60a8d738@googlegroups.com>

Show all headers | View raw


On Tue, 29 Sep 2015 02:27:23 -0700, plewto wrote:

> 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.
> 
> Originally I used the version which is now commented out. When a new
> instance was created it created an ID by appending the value of
> __instance_counter to the class name, it then checked the contents of of
> __instatance_registrty to see if this ID was already in use. If so it
> incremented the counter until it found an unused ID. This version works
> exactly as I expected.
> 
> Later I decided to get rid of __instance_registry and rely solely on the
> restricted access to __instance_counter and the fact that it is
> monotonically increasing to generate IDs. I wrote the second, simpler
> version of __gen_id to that end, but it doesn't work!  No doubt I'm
> overlooking something very simple here but I'm not seeing it.
> 
> Any help appreciated.
> 
> 
> 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 = ''
>     #     while id in self.__instance_registry:
>     #         id = '%s_%d' % (ty, self.__instance_counter)
>     #         self.__instance_counter += 1 #    
>     self.__instance_registry[id] = self #     return id
> 
>     def __gen_id(self):
>         ty = self.__class__.__name__
>         id = '%s_%d' % (ty, self.__instance_counter)
>         self.__instance_counter += 1 return id
> 
>     def __str__(self):
>         return "name = '%s'   id = '%s'" % (self.__name, self.__id)
>     
>     
> go1 = GameObject("GO1")
> go2 = GameObject("GO2")
> go3 = GameObject("GO3")
> print(go1)
> print(go2)
> print(go3)
> 
> 
> # Results with original __gen_id method # name = 'GO1'   id =
> 'GameObject_0'
> # name = 'GO2'   id = 'GameObject_1'
> # name = 'GO3'   id = 'GameObject_2'
> 
> 
> # Results with new simpler __gen_id method, __instance_counter not being
> incremented # name = 'GO1'   id = 'GameObject_0'
> # name = 'GO2'   id = 'GameObject_0'
> # name = 'GO3'   id = 'GameObject_0'

why reinvent the wheel?
why not simply use pythons builtin id function?
each new instance of an object is automatically assigned a unique ID


-- 
I'm a soldier, not a diplomat.  I can only tell the truth.
		-- Kirk, "Errand of Mercy", stardate 3198.9

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