Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Wolfgang Maier Newsgroups: comp.lang.python Subject: Re: static variables Date: Tue, 1 Dec 2015 10:58:50 +0100 Lines: 56 Message-ID: References: <565cf163$0$1612$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de Fn6Ren4IqQmYQgXtYEN6BArCUGLegErzFEd8yvRrTbxQ== 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; '(self,': 0.07; 'rename': 0.07; '(self):': 0.09; '__init__': 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; 'a()': 0.16; 'cleaner': 0.16; 'counter()': 0.16; 'function?': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'renaming': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'stick': 0.18; 'cheers,': 0.22; 'trying': 0.22; 'seems': 0.23; 'header:In-Reply-To:1': 0.24; 'wondering': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'function': 0.28; 'received:132': 0.29; 'allows': 0.30; "i'm": 0.30; 'print': 0.30; 'skip:_ 10': 0.32; 'implement': 0.32; 'class': 0.33; 'problem': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'something': 0.35; 'possible': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'several': 0.38; 'test': 0.39; 'to:addr:python.org': 0.40; 'more': 0.63; 'now:': 0.72; 'counts': 0.81; '__call__': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 132.230.194.214 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: 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: , Xref: csiph.com comp.lang.python:99783 On 01.12.2015 09:26, Ulli Horlacher wrote: > Steven D'Aprano wrote: > >> A better and more general test is: >> >> if hasattr(a, 'x'): print('attribute of a') > > Fine! > > I have now: > > def a(x=None): > if not hasattr(a,'x'): a.x = 0 > a.x += 1 > print('%d:' % a.x,x) > > This simply counts the calls of a() > > But, when I rename the function I have to rename the attribute also. > Is it possible to refer the attribute automatically to its function? > Something like: > > def a(x=None): > if not hasattr(_function_,'x'): _function_.x = 0 > _function_.x += 1 > print('%d:' % _function_.x,x) > > I'm wondering whether you have a good reason to stick with a function. What you are trying to achieve seems to be easier and cleaner to implement as a class: class Counter (object): def __init__ (self, start_value=0): self.x = start_value def __call__ (self): self.x += 1 1) solves the renaming problem 2) allows you to have several counters around: counter1 = Counter() counter2 = Counter() counter3 = Counter(35) counter1() counter2() counter1() print (counter1.x, counter2.x, counter3.x) Cheers, Wolfgang