Problem
I have some methods, which I want to use outside of a class directly and as well group them inside some different classes.
How to define a method outside of the class definition is already properly answered:
However, I found nothing about static methods.
What is the best way to do it?
My approaches
For a class like:
class some_class:
@staticmethod
def some_haevy_method(...):
...
I tried the following:
Decorator
def some_haevy_method(...):
...
class some_class:
@staticmethod
some_haevy_method = some_haevy_method
Doesn't compile: SyntaxError: invalid syntax
Replacing
def some_haevy_method(...):
...
class some_class:
@staticmethod
def some_haevy_method(...):
pass
some_haevy_method = some_haevy_method
Error: some_haevy_method () takes 0 positional arguments but 1 was given
(decorator is overwritten, self is passed)
Redefining
def some_haevy_method(...):
...
class some_class:
@staticmethod
def some_haevy_method(*args, **kwargs):
return some_haevy_method(*args, **kwargs)
This actually works, but the drawback is, that the documentation and type hinting is not available.
My use case
Edit: Since it was asked in comments.
I want to use my some_haevy_method
directly in scripts without even knowing about the class.
Also it will appear in some classes. And this classes have children and the children have other children. And every of them should be able to provide this functionality.
Probably it is a little lazy coding, since my classes are in this example more thematic grouped methods but it helps me in my project. For example when I am debugging, and I don't have to load the specific methods every time.
Feel free to roast me in the comments :)