Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99783
| From | Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: static variables |
| Date | 2015-12-01 10:58 +0100 |
| Message-ID | <mailman.60.1448963939.14615.python-list@python.org> (permalink) |
| References | <n3i06o$9ot$1@news2.informatik.uni-stuttgart.de> <n3ibkd$nl0$1@dont-email.me> <565cf163$0$1612$c3e8da3$5496439d@news.astraweb.com> <n3jljn$os2$1@news2.informatik.uni-stuttgart.de> |
On 01.12.2015 09:26, Ulli Horlacher wrote:
> Steven D'Aprano <steve@pearwood.info> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
static variables Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-30 17:15 +0000
Re: static variables Terry Reedy <tjreedy@udel.edu> - 2015-11-30 12:38 -0500
Re: static variables BartC <bc@freeuk.com> - 2015-11-30 20:32 +0000
Re: static variables Steven D'Aprano <steve@pearwood.info> - 2015-12-01 12:01 +1100
Re: static variables Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-12-01 08:26 +0000
Re: static variables Peter Otten <__peter__@web.de> - 2015-12-01 10:01 +0100
Re: static variables Grobu <snailcoder@retrosite.invalid> - 2015-12-01 10:15 +0100
Re: static variables Steven D'Aprano <steve@pearwood.info> - 2015-12-02 12:02 +1100
Re: static variables Erik <python@lucidity.plus.com> - 2015-12-02 01:16 +0000
Re: static variables Steven D'Aprano <steve@pearwood.info> - 2015-12-02 12:24 +1100
Re: static variables Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-12-02 09:21 +0100
Re: static variables Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-12-02 09:34 +0100
Re: static variables Steven D'Aprano <steve@pearwood.info> - 2015-12-02 21:22 +1100
Re: static variables Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-12-02 12:09 +0100
Re: static variables Steven D'Aprano <steve@pearwood.info> - 2015-12-03 00:11 +1100
Re: static variables Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-12-02 14:41 +0100
Re: static variables Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-02 13:48 +0000
Re: static variables Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-12-02 15:07 +0100
Re: static variables Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-02 08:15 -0600
Re: static variables Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-02 14:31 +0000
Re: static variables Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-12-02 16:30 +0100
Re: static variables Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-02 14:30 -0600
Re: static variables Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-12-03 10:06 +0100
Re: static variables Chris Angelico <rosuav@gmail.com> - 2015-12-02 20:23 +1100
Re: static variables Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-12-02 10:47 +0100
Re: static variables Marko Rauhamaa <marko@pacujo.net> - 2015-12-02 12:18 +0200
Re: static variables Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-12-02 11:56 +0100
Re: static variables Marko Rauhamaa <marko@pacujo.net> - 2015-12-02 13:30 +0200
Re: static variables Steven D'Aprano <steve@pearwood.info> - 2015-12-03 00:17 +1100
Re: static variables Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2015-12-01 10:58 +0100
Re: static variables Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-12-01 11:47 +0000
csiph-web