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")


Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source