Possible Duplicate:
Static class variables in Python
What is the Python equivalent of static variables inside a function?
How can I use static fields in Python ?
for example i want to count how many times the function has been called - how can i do this ?
Python doesn't have static variables but you can fake it by defining a callable class object and then using it as a function.
Static Members Only one copy of a static member exists, regardless of how many instances of the class are created.
When we declare a variable inside a class, but outside the method, it is called a static or class variable. It can be called directly from a class but not through the instances of a class.
Yes, definitely possible to write static variables and methods in python. Static Variables : Variable declared at class level are called static variable which can be accessed directly using class name.
When we declare a variable inside a class, but outside the method, it is called a static or class variable. It can be called directly from a class but not through the instances of a class. However, the static variables are quite different from the other member, and it does not conflict with the same variable name in the Python program.
For this, we can use the @staticmethod decorator to create methods in a class static. A decorator is a special function specified before a function and takes the whole function as a parameter. Note that this method only works on versions of Python higher than 2.6. The @classmethod can make a method static to the class and not its object.
We call these members static and they are independent of an instance. WARNING! Today's lesson will show you static members which actually violate the object-oriented principles. OOP includes them only for special cases and in general everything can be written without static members. We always have to think carefully before adding static members.
Unlike instance methods, static methods aren’t bound to an object. In other words, static methods cannot access and modify an object state. In addition, Python doesn’t implicitly pass the cls parameter (or the self parameter) to static methods. Therefore, static methods cannot access and modify the class’s state.
If you wish to count how many times a method has been called, no matter which instance called it, you could use a class member like this:
class Foo(object): calls=0 # <--- call is a class member def baz(self): Foo.calls+=1 foo=Foo() bar=Foo() for i in range(100): foo.baz() bar.baz() print('Foo.baz was called {n} times'.format(n=foo.calls)) # Foo.baz was called 200 times
When you define calls
this way:
class Foo(object): calls=0
Python places the key-value pair ('calls', 0) in Foo.__dict__
.
It can be accessed with Foo.calls
. Instances of Foo
, such as foo=Foo()
, can access it with foo.calls
as well.
To assign new values to Foo.calls
you must use Foo.calls = ...
. Instances can not use foo.calls = ...
because that causes Python to place a new and different key-value pair in foo.__dict__
, where instance members are kept.
Here is some example counting the number of calls of all objects of the same class:
class Swallow(): i = 0 # will be used for counting calls of fly() def fly(self): Swallow.i += 1
And this is the proof:
>>> a = Swallow() >>> b = Swallow() >>> a.fly() >>> a.i 1 >>> Swallow.i 1 >>> b.fly() >>> b.i 2 >>> Swallow.i 2
so you can read it by giving the object name or class name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With