I've been working with Python for a while and I find the syntax for declaring methods as static to be peculiar.
A regular method would be declared:
def mymethod(self, params)
...
return
A static method is declared:
def mystaticethod(params)
...
return
mystaticmethod = staticmethod(mystaticmethod)
If you don't add the static method line, the compiler complains about self missing.
This is a very complex way of doing something very simple that in other languages simply use a keyword and a declaration grammar to. Can anyone tell me about the evolution of this syntax? Is this merely because classes were added into the existing language?
Since I can move the staticmethod line to later in the class, it also suggests that the parser is working extra hard on bookkeeping.
Note that I'm aware of the decorator syntax that was added later, I'm interested to know how the original syntax came about from a language design perspective. The only think I can think of is that the staticmethod application invokes an operation that transforms the function object into a static method.
Static methods were added to Python long after classes were (classes were added very early on, possibly even before 1.0; static methods didn't show up until sometime about 2.0). They were implemented as a modification of normal methods — you create a static method object from a function to get a static method, whereas the compiler generates instance methods by default.
As with many things in Python, static methods got introduced and then refined as people used them and wanted better syntax. The initial round was a way of introducing the semantics without adding new syntax to the language (and Python is quite resistant to syntax changes). I'm not Guido, so I'm not exactly sure what was going on in his head and this is somewhat speculative, but Python tends to move slowly, develop incrementally, and refine things as they gain more experience with them (in particular, they don't like adding something until they've figured out the right way to do it. This could have been why there wasn't special syntax for static methods from the beginning).
As mjv indicated, though, there is an easier way now, through some syntax sugar added in 2.2 or 2.3 called "decorators":
@staticmethod
def mystaticmethod(params)
...
return
The @staticmethod
syntax is sugar for putting mystaticmethod = staticmethod(mystaticmethod)
after the method definition.