Hello @Anno - I have to say I have never seen anyone tinkering with Python the way you doā¦
Perhaps with āmergingā you mean āoverloadingā?
from typing import overload
@overload
def test(x: int, y: int):
... # this is the Ellipsis object written as "..."
@overload
def test(x: str, y: str):
....
def test(x, y):
if isinstance(x, int) and isinstance(y, int):
print(f"Hello world from two numbers who are now tightly merged: {x + y}")
elif isinstance(x, str) and isinstance(y, str):
print(f"Hello world from the strings `{x}` and `{y}` who are still separate")
else:
raise ValueError("Sorry, I can only test strings or ints")
But here you still need to write your own, single ātestā function that does all the work.
If you start from two given functions, test1
and test2
, the only way to āmergeā them (that I can think of) is to write yet another function, say either_test1_or_test2
that will call either test1
or test2
depending on whatever conditions are relevant:
def either_test1_or_test2(x, y):
if x > 0 and y > 0:
return test1(x, y)
else:
return test2(x, y)
There is another possible way of āmergingā. Perhaps you had this in mind?
You could call a function that takes another function as argument:
def test(func, *args):
return func(*args) # this is too simplistic too make practical sense,
# but is a pattern used for instance in functools.partial
Now you would have:
test(int, 42) == 42
test(str, 42) == "42"
test(test, int, 42) == 42
test(test, test, test, str, 42) == "42"
test(*(test, test, test, int, 42)) == 42