Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How was the syntax chosen for static methods in Python?

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.

like image 541
Uri Avatar asked Sep 25 '09 14:09

Uri


People also ask

How do you define a static method in Python?

What is a static method? Static methods in Python are extremely similar to python class level methods, the difference being that a static method is bound to a class rather than the objects for that class. This means that a static method can be called without an object for that class.

How do you call a static method in syntax?

A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables. Since the static method refers to the class, the syntax to call or refer to a static method is: class name. method name.

Why static method is used in Python?

Static methods are used when we don't want subclasses of a class change/override a specific implementation of a method.

Which statement about static methods is true Python?

Q: What statement about static methods is true? Static methods are called static because they always return None. Static methods can be bound to either a class or an instance of a class. Static methods serve mostly as utility methods or helper methods, since they can't access or modify a class's state.


1 Answers

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.

like image 195
Michael Ekstrand Avatar answered Sep 20 '22 16:09

Michael Ekstrand