On Apr 11, 5:19 am, "7stud" <[EMAIL PROTECTED]> wrote:
> On Apr 10, 9:08 pm, "人言落日是天涯,望极天涯不见家" <[EMAIL PROTECTED]> 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)

1. In this case you will prefer a classmethod instead a staticmethod.
2. If counter is the number of instances of class AAA then you will
incrase counter inside __init__ method.

class AAA (object):
    counter = 0
    def __init__(self):
        type(self).counter_increase()
    @classmethod
    def counter_increase(cls):
        cls.counter += 1

or

class AAA (object):
    counter = 0
    def __init__(self):
        type(self).counter += 1

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to