Does python have the static function member like C++

G

Guest

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 !
 
7

7stud

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)
 
7

7stud

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 !

You can also do this:

class AAA:
counter = 0
def __init__(self):
pass

AAA.counter += 1
print AAA.counter
 
B

bearophileHUGS

Many thanks for you!
I've never heard of the "staticmethod" , that's great!

Note that you don't need an empty __init__ :

class AAA:
counter = 0

@staticmethod
def counter_increase():
AAA.counter += 1
print "couter now:", AAA.counter

AAA.counter_increase()

Bye,
bearophile
 
G

goodwolf

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
 
B

Bruno Desthuilliers

人言è½æ—¥æ˜¯å¤©æ¶¯ï¼Œæœ›æžå¤©æ¶¯ä¸è§å®¶ 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
 
B

Bruno Desthuilliers

goodwolf a écrit :
(snip)
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()

You can call a class method on an instance:
self.counter_increase()

And FWIW, this is probably something I'd put in the constructor (the
__new__ method), not in the initializer.
@classmethod
def counter_increase(cls):
cls.counter += 1

or

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

Instances have a reference to their class, so you can also write this:
self.__class__.counter += 1
 
G

goodwolf

goodwolf a écrit :
(snip)



You can call a class method on an instance:
self.counter_increase()

And FWIW, this is probably something I'd put in the constructor (the
__new__ method), not in the initializer.


Instances have a reference to their class, so you can also write this:
self.__class__.counter += 1

OK, you will use something like this:

class AAA (object):
counter = 0
def __new__(cls):
cls.counter += 1
return super(cls, cls).__new__(cls)

but I think that __new__ is more "low level" and not necessary here,
so I will use:

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

with yours correction invoking self.counter_increase() instead of more
explicit type(self).counter_increase()
 
B

Bruno Desthuilliers

goodwolf a écrit :
OK, you will use something like this:

class AAA (object):
counter = 0
def __new__(cls):
cls.counter += 1
return super(cls, cls).__new__(cls)

return super(AAA, cls).__new__(cls)
but I think that __new__ is more "low level" and not necessary here,

It's of course 'not necessary'. But (IMHO):
- increasing the class's instance counter is more a responsability of
the class than a responsability of the instance - and it has nothing to
do with initializing the instance's state
- if someone is to subclass AAA, there are fewer chances that he'll
override the constructer than the initializer, and if he does, there are
more chances that he won't forget to call on the parent's constructor.

IOW, this is actually *because* it is 'lower level' that I think it's a
better place for such operations.

But YMMV, of course !-)

My 2 cents...
 
G

goodwolf

goodwolf a écrit :






return super(AAA, cls).__new__(cls)


It's of course 'not necessary'. But (IMHO):
- increasing the class's instance counter is more a responsability of
the class than a responsability of the instance - and it has nothing to
do with initializing the instance's state
- if someone is to subclass AAA, there are fewer chances that he'll
override the constructer than the initializer, and if he does, there are
more chances that he won't forget to call on the parent's constructor.

IOW, this is actually *because* it is 'lower level' that I think it's a
better place for such operations.

But YMMV, of course !-)

My 2 cents...

OK, but then you will use an more flexible constructor:

class AAA (object):
counter = 0
def __new__(cls, *args, **kwargs):
cls.counter += 1
return super(AAA, cls).__new__(cls, *args, **kwargs)

then you see that you will pay lot resources for a "lower level"
operation.

However if your counter is to consider a low level think, than You are
right, but it's more a personal consideration.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top