Does python have the static function member like C++
人言落日是天涯,望极天涯不见家
kelvin.you at gmail.com
Tue Apr 10 23:34:42 EDT 2007
On Apr 11, 11:19 am, "7stud" <bbxx789_0... at yahoo.com> wrote:
> On Apr 10, 9:08 pm, "人言落日是天涯,望极天涯不见家" <kelvin.... at gmail.com> wrote:
>
> > 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
>
> > But how could I call the function "counter_incrrease" ?
>
> > Thanks !
>
> 1)
> class AAA:
> counter = 0
> def __init__(self):
> pass
> @staticmethod
> def counter_increase():
> AAA.counter += 1
> print "couter now :", AAA.counter
>
> AAA.counter_increase()
>
> 2)
> class AAA:
> counter = 0
> def __init__(self):
> pass
> def counter_increase():
> AAA.counter += 1
> print "couter now :", AAA.counter
> counter_increase = staticmethod(counter_increase)
>
> AAA.counter_increase()
>
> 3)
> class AAA:
> counter = 0
> def __init__(self):
> pass
> def counter_increase(self):
> AAA.counter += 1
> print "couter now :", AAA.counter
> aaa = AAA()
> AAA.counter_increase(aaa)
Many thanks for you!
I've never heard of the "staticmethod" , that's great!
More information about the Python-list
mailing list