On 22 Aug 2020, at 15:00, email@example.com wrote:
I wanted to know if there could be TypeError when giving wrong type for arguments in functions (This usually happens when using other module's function)
def sum(nom1: int, nom2: int): nom = nom1 + nom2 return nom print(sum('hello',2))
if you run this code you will get TypeError for line 2 because 'you can only concatenate str (not "int") to str' But what am i saying is can it raise TypeError for line 4 because I gave 'hello' as nom1 and nom1 should be int
Now if I want to check arguments types I should use:
def sum(nom1: int, nom2: int): if isinstance(nom1, int) and isinstance(nom1, int): nom = nom1 + nom2 return nom else: raise TypeError('nom1 and nom2 should be int') print(sum('hello',2))
But if they can add what am I am i saying it can decrease lines of this function by 50% and also function author should not worry about checking types anymore! (I know I could use 'assert' but I just wanted to write it as simple as possible)
Python does not check type annotations at runtime, although you could write a decorator that does this for you. It is unlikely that this will anytime soon.