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)