0

Type() function

Is used to verify the type of any object/variable...what instance is this used? Coz I don't see why someone will verify the type of variable/object he/she created, that's why I feel there's more to the type() function than I know

11th Dec 2018, 6:22 PM
Joseph Ojo
Joseph Ojo - avatar
2 odpowiedzi
+ 4
This is one of the "bad" of untypized variable. Suppose that you have a function that expect a parameter of type MyClass and call an method myMethod on this object. What calling code pass another type to this function? You can accept it and keep that interpreter raise an error or test the type of param and make your decision about how you have to treat this "problem". I think that last option is better because offer you more control on decision and is for this that type() exists
11th Dec 2018, 6:58 PM
KrOW
KrOW - avatar
+ 1
For example you can use it to check if the argument of a function is what is needed. Let's say your function f needs an integer, then you could ask: if type(n) != int: ... And then execute a fitting reaction, like raising an error or something.
11th Dec 2018, 6:37 PM
HonFu
HonFu - avatar