Path: csiph.com!news.swapon.de!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!feeds.phibee-telecom.net!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; 'value,': 0.03; 'subject:Question': 0.05; 'message-id:@4ax.com': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; '"while"': 0.16; '>that': 0.16; 'dummy': 0.16; 'increment': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'registry': 0.16; 'subject:class': 0.16; 'subject:variable': 0.16; "wouldn't": 0.16; 'duplicate': 0.18; 'url:home': 0.18; 'variable': 0.18; '2015': 0.20; 'sep': 0.22; 'pass': 0.22; 'this:': 0.23; 'all.': 0.24; "doesn't": 0.26; 'header:X-Complaints-To:1': 0.26; 'function': 0.28; 'values': 0.28; 'initialized': 0.29; 'searches': 0.29; 'starts': 0.29; 'skip:_ 10': 0.32; 'class': 0.33; 'instances': 0.33; 'tue,': 0.34; 'clear': 0.35; 'quite': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'received:org': 0.37; 'charset:us-ascii': 0.37; 'version': 0.38; 'drop': 0.38; 'self': 0.38; 'shared': 0.38; 'why': 0.39; 'does': 0.39; 'enough': 0.39; 'to:addr:python.org': 0.40; 'local,': 0.84; 'dennis': 0.91; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: Question re class variable Date: Tue, 29 Sep 2015 08:21:14 -0400 Organization: IISS Elusive Unicorn References: <3948d9cd-24b1-4a3b-8ed0-46bb60a8d738@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-108-68-178-61.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1443529284 news.xs4all.nl 23749 [2001:888:2000:d::a6]:33051 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:97208 On Tue, 29 Sep 2015 14:17:35 +0300, Anssi Saari 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/