Skip to content Skip to sidebar Skip to footer

Python - Should I Use Static Methods Or Top-level Functions

I come from a Java background and I'm new to python. I have a couple scripts that share some helper functions unique to the application related to reading and writing files. Some

Solution 1:

In Java there's the (IMHO wrong) idea to use classes everywhere, even just group together static functions that don't share any state (and thus such classes will never be instantiated).

Python here begs to differ; if you have functions that don't have some shared state (and thus in Java would typically be static functions) and aren't tightly related to a "real" class (=one that is actually instantiated) you just use free functions inside a module.

The reasoning behind this is that a class is needed only when you actually want to instantiate it, thus having a class just as a container for several functions that don't need to share an instance-specific state is useless.

Actually, you can somewhat think of a module as a static class - i.e. a container of functions (=static methods), module variables (=static fields) and types.

The nice thing in Python is that having top-level function doesn't give global namespace pollution problems, since in Python top-level functions/objects/... are still module-scoped. Thus, you can still group functions by module, without the unnecessary class-tax.


  1. actually, they can have some shared state, in form of module-level variables (so, singletons); again, the analogy modules-static classes seems to hold.

Solution 2:

From the Zen of Python (import this) :

Namespaces are one honking great idea -- let'sdo more of those!

One of the main reasons to create static methods in a language like Java is to ensure that those methods don't wind up polluting the global namespace. (Although Java enforces its own namespace conventions by disallowing "package-level" functions entirely !) In Python, all "top-level" functions are automatically placed within the namespace of the module containing those functions, so there's no danger of polluting the global namespace this way.

In other words, like many other languages, Python can create namespaces in a few different ways. In this case, there's little need to create a class containing only static methods, when a module serves the same namespacing purpose without the clutter (or cognitive load) associated with defining a class.

Solution 3:

It a question of namespace pollution. If you have a module with multiple classes and some functions that only make sense for a certain class and its descendents, then make that a static method. The static method can be called either by using the class name or by using an object of the class.

>>>classA(object):...    @staticmethod...defstatic_1():...print'i am static'...defa(self):...            self.static_1()...>>>A.static_1()
i am static
>>>a=A()>>>a.a()
i am static
>>>a.static_1()
i am static
>>>classB(A):...pass...>>>b=B()>>>b.static_1()
i am static
>>>

Solution 4:

If the function is related to a class, then make it a static method. For example:

classDBobject():
    name = StringProperty()

    @staticmethoddefget_by_name(name):
        return db.select('select * from DBobject where name = "%s"' % name)

python = DBobject.get_by_name('python')

That is, the method is completely related to the DBobject class so it should be a static method.

Solution 5:

One other aspect of Python static methods is that when called on instances, they are polymorphic on the type of the variable they are called on, despite not having an instance of self.

For example:

classBaseClass:@staticmethoddefmy_method():
        return0classSubclassLeft:@staticmethoddefmy_method():
        return"left"classSubclassRight:@staticmethoddefmy_method():
        return"right"


instances = [BaseClass(), SubclassLeft(), SubclassRight()]

for i ininstances:
    print(i.my_method())

Running this results in

$ python example.py0leftright

Post a Comment for "Python - Should I Use Static Methods Or Top-level Functions"