arrow in python function

Solutions on MaxInterview for arrow in python function by the best coders in the world

showing results for - "arrow in python function"
Alistair
19 Jan 2019
1"""
2The arrow in python functions denotes the return value of the function. Note
3that the arrow doesn't enforce anything, and nothing prevents a developer from
4returning some value that isn't what is indicated. Obviously, this is bad
5practice, and should never happen.
6
7In the example below c is gotten from a function that claims to return an int,
8but instead returns a float. Despite this, Python has no problem using the
9float methods in c.
10"""
11
12def int_returner() -> int:
13    return 1
14
15def float_returner() -> float:
16    return 1.0
17
18def fake_int_returner() -> int:
19    return 1.0
20
21
22a = int_returner()
23b = float_returner()
24c = fake_int_returner()
25
26print(type(a)) #int
27print(type(b)) #float
28print(type(c)) #float (function hints an 'int')
29
30# is_integer() is a built-in function for floats that integers don't have.
31try:
32    a.is_integer()
33except:
34    print ("a has no method 'is_integer' meaning it isn't of type float")
35try:
36    b.is_integer()
37except:
38    print ("b has no method 'is_integer' meaning it isn't of type float")
39try:
40    c.is_integer()
41except:
42    print ("c has no method 'is_integer' meaning it isn't of type float")
43
44