2

I know how to overload a function and how to caerte a static function in python3.

While now, I want to overload a staticmethod in python3. am I doing it right?

class MyClass:
    @overload
    @staticmethod
    def MyDef(x: int) -> None: ...

    @overload
    @staticmethod
    def MyDef(x: str) -> None: ...

    @staticmethod
    def MyDef(x):
        pass

1 Answers1

-3

The link in the first comment to this answer shows the implementation using the overload decorator. Here is the format of code for this:

@overload
def process(response: None) -> None:
    ...
@overload
def process(response: int) -> tuple[int, str]:
    ...
@overload
def process(response: bytes) -> str:
    ...
def process(response):
    <actual implementation>

This format given in the question appears to be consistent with this so would be fine.

A quick note on overloading:

Function overloading is a feature of object oriented programming where two or more functions can have the same name but different parameters.

Python does not support function overloading. When we define multiple functions with the same name, the later one always overrides the prior and thus, in the namespace, there will always be a single entry against each function name.

This link might be helpful.

Python function overloading

The process offerred for overloading that python offers is via the overload decorator in the above way.

D.L
  • 4,339
  • 5
  • 22
  • 45