1

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 :)

CodePrinz
  • 445
  • 5
  • 8
  • 3
    If you already have a global function, it seems entirely unnecessary to define a static method. – chepner Jan 07 '22 at 15:29
  • Why do you want to have a static function outside of a class? – Robert Jan 07 '22 at 15:30
  • @chepner: I want to use it via the class and not reload the specific function every time. I will update with some example use. – CodePrinz Jan 07 '22 at 15:34
  • @Robert Why not? This particular method could maybe be used from other modules without knowing about the class. – CodePrinz Jan 07 '22 at 15:34
  • 1
    Nothing gets reloaded. Anywhere you would have used `some_class.some_heavy_method`, you can just as easily use `some_heavy_method`. Actual use cases for static methods are fairly uncommon. – chepner Jan 07 '22 at 15:37

1 Answers1

2

Decorator syntax is only used for function and class definitions, but is only necessary in those cases to avoid repetition of names. The syntax is just a shortcut for assignments, so you can write

def some_heavy_method(...):
      ...

class some_class:    
    some_heavy_method = staticmethod(some_heavy_method

The decorator syntax is always just a convenience; without it, you could always simply write

class some_class:

    def f():
        ...

    f = staticmethod(f)
chepner
  • 497,756
  • 71
  • 530
  • 681