Answers for "arrow python"

2

arrow in python function

"""
The arrow in python functions denotes the return value of the function. Note
that the arrow doesn't enforce anything, and nothing prevents a developer from
returning some value that isn't what is indicated. Obviously, this is bad
practice, and should never happen.

In the example below c is gotten from a function that claims to return an int,
but instead returns a float. Despite this, Python has no problem using the
float methods in c.
"""

def int_returner() -> int:
    return 1

def float_returner() -> float:
    return 1.0

def fake_int_returner() -> int:
    return 1.0


a = int_returner()
b = float_returner()
c = fake_int_returner()

print(type(a)) #int
print(type(b)) #float
print(type(c)) #float (function hints an 'int')

# is_integer() is a built-in function for floats that integers don't have.
try:
    a.is_integer()
except:
    print ("a has no method 'is_integer' meaning it isn't of type float")
try:
    b.is_integer()
except:
    print ("b has no method 'is_integer' meaning it isn't of type float")
try:
    c.is_integer()
except:
    print ("c has no method 'is_integer' meaning it isn't of type float")
Posted by: Guest on August-08-2021

Python Answers by Framework

Browse Popular Code Answers by Language