Does python have the static function member like C++

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Wed Apr 11 03:05:41 EDT 2007


人言落日是天涯,望极天涯不见家 a écrit :
> I define the class like this:
> class AAA:
>     counter = 0
>     def __init__(self):
>         pass
>     def counter_increase():
>         AAA.counter += 1
>         print "couter now :", AAA.counter

You probably want something like this:

class AAA(object):
     _counter = 0

     @classmethod
     def increase_counter(cls):
         cls._counter += 1
         print "%s._counter is now %d" % (cls.__name__, cls._counter)

> But how could I call the function "counter_incrrease" ?

With the above correction, you can call it eiter on the class or on an 
instance:

AAA.increase_counter()
aaa = AAA()
aaa.increase_counter()

HTH



More information about the Python-list mailing list