Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static member of a function in Python ? [duplicate]

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 ?

like image 751
Patryk Avatar asked Nov 13 '11 12:11

Patryk


People also ask

What is the equivalent of static in Python?

Python doesn't have static variables but you can fake it by defining a callable class object and then using it as a function.

How many possible copies can a static have?

Static Members Only one copy of a static member exists, regardless of how many instances of the class are created.

What is static data member in Python?

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.

Is there static variable in Python?

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.

What is a static variable in Python?

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.

How to create a static method in a class in Python?

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.

What are static members in JavaScript?

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.

What is the difference between instance and static methods in Python?

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.


2 Answers

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.

like image 129
unutbu Avatar answered Oct 09 '22 20:10

unutbu


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.

like image 31
Tadeck Avatar answered Oct 09 '22 21:10

Tadeck