Static in python

By: Python Documentation Team Emailed: 1765 times Printed: 2513 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Both static data and static methods (in the sense of C++ or Java) are supported in Python. For static data, simply define a class attribute. To assign a new value to the attribute, you have to explicitly use the class name in the assignment:

class C:
    count = 0   # number of times C.__init__ called

    def __init__(self):
        C.count = C.count + 1

    def getcount(self):
        return C.count  # or return self.count


c.count also refers to C.count for any c such that isinstance(c, C) holds, unless overridden by c itself or by some class on the base-class search path from c.__class__ back to C.

Caution: within a method of C, an assignment like self.count = 42 creates a new and unrelated instance named “count” in self‘s own dict. Rebinding of a class-static data name must always specify the class whether inside a method or not:

C.count = 314


Static methods are possible since Python 2.2:

class C:
    def static(arg1, arg2, arg3):
        # No 'self' parameter!
        ...
    static = staticmethod(static)


With Python 2.4’s decorators, this can also be written as

class C:
    @staticmethod
    def static(arg1, arg2, arg3):
        # No 'self' parameter!
        ...


However, a far more straightforward way to get the effect of a static method is via a simple module-level function:

def getcount():
    return C.count


If your code is structured so as to define one class (or tightly related class hierarchy) per module, this supplies the desired encapsulation.


Python Home | All Python Tutorials | Latest Python Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Python Documentation Team
with in Python
goto in Python
Garbage Collection in Python
switch case statement in Python
Overloading in python
Static in python
Delegation in python
Schwartzian Transform in python
Multidimensional list (array) in python
Array in python
Remove duplicates from a list in python
Iterate over a sequence in reverse order in python
Convert between tuples and lists in python
Perl's chomp() equivalent for removing trailing newlines from strings in python
Convert number to string in python

More Tutorials in Python
What is the need for Python language?
How to compile python script and create .pyc file?
How to find the current module name in python
How to force rereading of a changed module in python
UnboundLocalError in python
call by reference in python
Callable objects in python
Ternary operator in python
Hexadecimal and Octal integers in python
Convert string to number in python
Convert number to string in python
Perl's chomp() equivalent for removing trailing newlines from strings in python
Convert between tuples and lists in python
Iterate over a sequence in reverse order in python
Remove duplicates from a list in python

More Latest News
Most Viewed Articles (in Python )
Static in python
Installing gedit for python programming in Windows
Installing gedit for python programming in Linux
Your First Program in Python
Comments And Pound Characters in Python
Numbers And Math in Python
Variables And Names in Python
Variables And Printing in Python
Strings And Text in Python
Printing in Python
Formatted printing in Python
Using Unicode Literals in Python
Unknown command: 'migrate'
error: â_mysql_ConnectionObjectâ has no member named
What is the need for Python language?
Most Emailed Articles (in Python)
Convert number to string in python
How to force rereading of a changed module in python
Hexadecimal and Octal integers in python
Formatted printing in Python
Ternary operator in python
Convert string to number in python
Remove duplicates from a list in python
Your First Program in Python
Numbers And Math in Python
Variables And Names in Python
error: â_mysql_ConnectionObjectâ has no member named
call by reference in python
Perl's chomp() equivalent for removing trailing newlines from strings in python
Iterate over a sequence in reverse order in python
Schwartzian Transform in python