I'm wondering, when do you use var: int and when do you use int(var).
For example:
def func(var: int, var2):
print(var - int(var2))
I'm wondering, when do you use var: int and when do you use int(var).
For example:
def func(var: int, var2):
print(var - int(var2))
var: int declares the type of a variable that is expected to be and pass to the desired function or class, while int(var) will cast var variable to be instance of int class:
>> a = '3' # a is String
>> a = int(a)
>> a
out: 3 # a is Ineteger now
var: int does essentially nothing unless you explicitely run a type checker (e.g. mypy) on your code. Without said typechecker it's just some metadata.
int(var) however is an actual and explicit runtime conversion. It will do nothing if var is already an int, but otherwise will attempt to convert var to an integer somehow (e.g. parsing the string or calling the object's __index__), and signal an error if it can not.