Answers for "arrow functions in 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
0

lambda function in python

In Python, an anonymous function is a function that is defined without a name.

While normal functions are defined using the def keyword in Python, 
anonymous functions are defined using the lambda keyword.

Hence, anonymous functions are also called lambda functions.

# syntax of Lambda function
lambda arguments: expression

# Program to show the use of lambda functions
double = lambda x: x * 2

print(double(5))
Posted by: Guest on December-31-2020

Python Answers by Framework

Browse Popular Code Answers by Language