Is it possible to have static methods in Python so I can call them without initializing a class, like:
ClassName.StaticMethod ( )
Is it possible to have static methods in Python so I can call them without initializing a class, like:
ClassName.StaticMethod ( )
Aside from the particularities of how static method objects behave, there is a certain kind of beauty you can strike with them when it comes to organizing your module-level code.
...
...
...
It now becomes a bit more intuitive and self-documenting in which context certain components are meant to be used and it pans out ideally for naming distinct test cases as well as having a straightforward approach to how test modules map to actual modules under tests for purists.
I frequently find it viable to apply this approach to organizing a project's utility code. Quite often, people immediately rush and create a
utils
package and end up with 9 modules of which one has 120 LOC and the rest are two dozen LOC at best. I prefer to start with this and convert it to a package and create modules only for the beasts that truly deserve them:In Python 3:
Here is another good to use non-static methods as they were static methods.
Call a non-static method as static
I think that Steven is actually right. To answer the original question, then, in order to set up a class method, simply assume that the first argument is not going to be a calling instance, and then make sure that you only call the method from the class.
(Note that this answer refers to Python 3.x. In Python 2.x you'll get a
TypeError
for calling the method on the class itself.)For example:
In this code, the "rollCall" method assumes that the first argument is not an instance (as it would be if it were called by an instance instead of a class). As long as "rollCall" is called from the class rather than an instance, the code will work fine. If we try to call "rollCall" from an instance, e.g.:
however, it would cause an exception to be raised because it would send two arguments: itself and -1, and "rollCall" is only defined to accept one argument.
Incidentally, rex.rollCall() would send the correct number of arguments, but would also cause an exception to be raised because now n would be representing a Dog instance (i.e., rex) when the function expects n to be numerical.
This is where the decoration comes in: If we precede the "rollCall" method with
then, by explicitly stating that the method is static, we can even call it from an instance. Now,
would work. The insertion of @staticmethod before a method definition, then, stops an instance from sending itself as an argument.
You can verify this by trying the following code with and without the @staticmethod line commented out.
You don't really need to use the
@staticmethod
decorator. Just declaring a method (that doesn't expect the self parameter) and call it from the class. The decorator is only there in case you want to be able to call it from an instance as well (which was not what you wanted to do)Mostly, you just use functions though...