static variables

Ulli Horlacher framstag at rus.uni-stuttgart.de
Tue Dec 1 03:26:31 EST 2015


Steven D'Aprano <steve at 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)


-- 
Ullrich Horlacher              Server und Virtualisierung
Rechenzentrum IZUS/TIK         E-Mail: horlacher at tik.uni-stuttgart.de
Universitaet Stuttgart         Tel:    ++49-711-68565868
Allmandring 30a                Fax:    ++49-711-682357
70550 Stuttgart (Germany)      WWW:    http://www.tik.uni-stuttgart.de/



More information about the Python-list mailing list