Zend's Coding Standard Naming Convention says

Functions in the global scope (a.k.a "floating functions") are permitted but discouraged in most cases. Consider wrapping these functions in a static class.

The common wisdom in Python says practically the opposite:

Finally, use staticmethod sparingly! There are very few situations where static-methods are necessary in Python, and I've seen them used many times where a separate "top-level" function would have been clearer.

(Not only does the above StackOverflow answer warn against overuse of static methods, but more than one Python linter will warn the same.)

Is this something that can be generalized across programming languages, and if so, why does Python differ so from PHP? If it's not something that can be generalized, what is the basis for one approach or the other, and is there a way to immediately recognize in a language whether you should prefer bare functions or static methods?

  • In python, you have to decorate the method to make it static, whereas in other languages (e.g. C#), static is a first-class keyword. Dec 18, 2012 at 19:11
  • @RobertHarvey That's not the reason though.
    – user7043
    Dec 18, 2012 at 19:12
  • @delnan: But it's a good indicator. Zend Framework is written in PHP, and the two languages couldn't be more diametrically opposed. In PHP the philosophy is more than one way to do it, whereas in Python the philosophy is only one way to do it, if possible. Dec 18, 2012 at 19:14

3 Answers 3


the basic difference between php global functions and their python counterpart is in their scope:

python top-level functions are part of the module where they are defined. when they are imported, they are called with defining_module.function_name() therefore there is less risk of conflicting names.

php 'global' functions are really global, iirc, no matter from where they are called. thus they are much more likely to create naming problems, e.g. be overwritten by some third-party library, etc.

  • PHP has namespaces. Dec 18, 2012 at 19:29
  • 2
    @Florian: While this is good, OP explicitly refers to 'floating functions' in the global scope
    – kr1
    Dec 18, 2012 at 20:20
  • This. I've actually had a site break because a new version of PHP the hoster upgraded to introduced a global function with the same name as an existing function from my code. This resulted in PHP aborting silently as soon as the interpreter encountered it. Rather nasty to debug... Dec 18, 2012 at 21:35

In general, it's a preference thing. "Static" in this context is just another word for "global", so although either way will get you some evil looks (particularly if you're only using classes as namespaces), it works.


  • PHP can very easily be set up to autoload classes, making it so that static functions aren't loaded in til you have need of them (or others in the same class). It can't do the same for individual functions (yet), though. I'm not sure whether Python has an equivalent for either one, but i haven't seen it yet.

  • Unlike global functions, static member functions can generally be made private, so it's easier to expose only the bits you intend for outsiders to use. Well, except in Python, where you have to jump through some hoops to get private members.

  • PHP didn't have namespace support til recently, meaning every top-level function was crammed into the same namespace. That's changed, but PHP namespaces are still rather hideous. :P

  • It seems Python doesn't like making member functions static. Other languages have a keyword for that purpose; in Python, apparently you need an annotation (excuse me, "decorator").

  • Some notes: Python has no equivalent to autoloaders. Marking something private is trivial in Python, for both module-level functions and class/instance attributes and methods (prepend a single underscore to the name). While it's true that we don't like static methods, we don't avoid a keyword for it because we couldn't, but because we don't need one (staticmethod is just library code, it doesn't need any changes to language semantics).
    – user7043
    Dec 18, 2012 at 19:33

Python namespaces everything: Everything is in a module, and everything defined in a module has module scope rather than global scope (you have to go out of your way and do evil things to add something to "all" scopes). So name collisions aren't an issue. In PHP, on the other hand, if you just go ahead and define a function, you put it in the same scope everyone is using for global names. You either explicitly use a namespace to prevent that, or you use a class as "namespace" (though the class should probably be in a namespace itself). The added bonus is that classes benefit from autoloaders, whereas functions do not IIRC.

There are philosophical differences too. The Python community is (and long has been) very open to utilizing multiple paradigms. Classes, higher-order functions and simple procedural programming are all equally accepted ways of doing stuff, as long as they fit the problem. And module-level data + functions does just what, so unless you need polymorphism or want a strong association with a particular class, there's little benefit of attaching a non-method to a class. In contrast, the PHP community (at least since the majority stopped screwing around and giving PHP the bad reputation it built up over the years) seems to be focused heavily on object-oriented programming. Free functions simply don't fit into this paradigm, whereas static is widely accepted. I don't quite get the logic behind that, but such is the convention.

Finally, note that staticmethod is not only eschewed because we don't like the concept of static methods. Sometimes we do want static methods, but then we usually prefer classmethod, which does basically the same thing but gives more power to the method (it receives the class object, and can thus there can be polymorphism among classmethods). staticmethod is sometimes considered to be entirely redundant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.