Let's say there is a class SomeClass with function func which returns int. How can we implement a similar interface?
class SomeClass:
@overload
@property
def f(self) -> int:
return 1
@overload
def f(self, x: int) -> int:
return x
if __name__ == "__main__":
obj = SomeClass()
print(obj.f) # print 1
print(obj.f(5)) # print 5
Preferably a solution without external modules, but if this is problematic, it is acceptable.
P.S. overload decorator doesn't exist, this is just an example.
Edit:
Function overloading is given only as an example implementation, but it is not necessary. The main goal is to implement this interface type:
obj = SomeClass()
obj.f # returns 1 (without brackets, works like property)
obj.f(5) # returns 5 (with brackets, works like a normal function)
@overloadin Python, but you can't use it to haveobj.fbe both anintand a callable.*args, **kwargsto have a method that behaves differently (and has different type signatures) depending on the type of its arguments. But that still doesn't help OP with what they're trying to do. That requires significantly more shenanigans.