Skip navigation

From google cache

 pythonhacker  pythonhacker is offline
Junior Member
Join Date: Feb 2007
Posts: 11
Default Re: Does Python have static methods ? If not what is the equivalent ?
Python does not have many of the frills and bits that languages like C++ and Java add to OOP.
For example, Python does not have access specifiers like public, protected & private. All member
functions and data are public by default. Privateness can be enforced by certain Pythonic tricks
such as prefixing private attributes with a '__' (double underscore) etc.

In a similar fashion, Python does not have static data members. Any data member defined at the
class level defaults to static in Python. Also since Python has no support for qualifiers (since it is
a dynamic language) it is not easy to support such syntax.

However Python does have support for static functions though they are not exactly the same as
those in C++ or Java. In Python a static function is nothing but a function that is unbounded i.e
it does not define the explicit "self" as the first argument. It also needs to be converted to a static
method by using the "staticmethod" typing for functions. This can be done by an explicit cast
after the function definition or by using a decorator (Python 2.4 & later).


# Before Python 2.4
class C(object):

def func():
print 'Hi, I am a function!'

func = staticmethod(func)

# Python 2.4 or later
class C(object):

def func()
print 'Hi, I am a function!'

This can be called on the class or on an instance similar to C++ or Java.

>>> C.func()
'Hi, I am a function!'
>>> c = C()
>>> c.func()
'Hi, I am a function!'

Just like C++ or Java, a static function can only access static attributes, not non-static ones.

>>> class C(object):
... X = 100
... @staticmethod
... def func():
... print 'Hi I am a function'
... print C.X
... # Try calling a non-static member
... self.func2()
... def func2(self):
... print 'This is function number 2!'

>> c= C()
>>> c.func()
Hi I am a function
Traceback (most recent call last):
File "", line 1, in ?
File "", line 8, in func
NameError: global name 'self' is not defined

This is because all instance functions have to define 'self' as an explicit first parameter. This is not
there for static functions.

A point to note is that if you try to convert an instance function to a static member function using
@staticmethod, Python silently allows you to do it. But the function is "lost", i.e it becomes uncallable
from either the instance level or class level because the interpreter gets confused.

>>> class C(object):
... @staticmethod
... def func(self):
... print 'Hi I am a function!'
>>> c=C()
>>> c.func()
Traceback (most recent call last):
File "", line 1, in ?
TypeError: func() takes exactly 1 argument (0 given)
>>> C.func()
Traceback (most recent call last):
File "", line 1, in ?
TypeError: func() takes exactly 1 argument (0 given)

This is because, staticmethod conversion is normally done for methods which do not define
the explicit 'self' parameter. When you did the conversion, the interpreter wraps up the original
function in a method wrapper which removes the 'self' from the list of arguments. However the
original function still has 'self' as the first argument. The method wrapper does not pass it.
The error comes out of it.

So be careful when writing static member functions in Python


One Comment

  1. Does your blog have a contact page? I’m having
    a tough time locating it but, I’d like to send you an e-mail.

    I’ve got some creative ideas for your blog you might be interested in hearing.
    Either way, great blog and I look forward to seeing it expand over time.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: