Functions in Python - return, scope, args and kwargs


Return in Function

Function execution stops once it reaches a return statement.

def add_num(x1,x2):
    return x1+x2
add_num(1,2)
Output:
3

The function will return 20, the addition expression will not execute as before that a return statement is reached.

def add_num(x1,x2):
    return 20
    return x1+x2
add_num(3,4)
Output:
20

Scoping in Function

  • Access global variable in function context

Global variables can be accessed but can`t be altered. To altered the same, they has to be accessed by using global in front of them. We’ll see that later in the same section.

i = 20
def add_num(x1):
    return i+x1
add_num(2)
Output:
22
print(i)
Output:
20

  • Declare a local variable within in a function

The scope of a local variable is limited to function itself. Outside the function it can`t be accessed and doing same will throw an error.

def add_num(x1):
    p = 40
    return p+x1

add_num(40)
Output:
80

Printing the local variable outside the function scope will throw an error.

try:
    print(p)
except Exception as e:
    print("Found Error - {0}".format(e))
Output:
Found Error - name 'p' is not defined

  • Declare local variable with same name as in global

Local varibale will be given preference if a variable with sam name as the one outside the function is decalred but its scopre is limited in the function

i = 20
def add_num(x1):
    i =10
    return i+x1
add_num(4)
Output:
14

This will print the value of global variable.

print(i)
Output:
20

  • Using Global Keyword

Global variable can`t be altered, to do the same access them via global keyword

i = 20
def add_num(x1):
    i = i -20
    return i+x1

try:
    add_num(5)
except Exception as e:
    print("Found Error - {0}".format(e))
Output:
Found Error - local variable 'i' referenced before assignment
i = 20
def add_num(x1):
    global i
    i = i -20
    return i+x1
add_num(5)
Output:
5

Accessing with global, will altered there value as well.

print(i)
Output:
0

args and kwargs in function

  • args

User define arguments and keyword arguments (name, value pair) can be defined in function using *args and **kwargs. The *args have to be defined after positional argument and kwargs has to be defined after *args. There is a strict order is defined if any function incldue these all.

def print_name(*args):
    for argument in args:
        print(argument)

print_name("hello","world!")
Output:
hello
world!

  • kwargs
def print_name(**kwargs):
    print(kwargs)
    print(kwargs.items())
    for item in kwargs:
        print(item, kwargs[item])
    for name, value in kwargs.items():
        print( '{0} = {1}'.format(name, value))

print_name(a = "hello",b="world!")
Output:
{'a': 'hello', 'b': 'world!'}
dict_items([('a', 'hello'), ('b', 'world!')])
a hello
b world!
a = hello
b = world!

Jupyter Notebook Link - Functions using apply, applymap and map{:target="_blank"}


Related Posts

Applying functions over pandas dataframe using apply, applymap and map

April 23, 2019

Read More
Break, Continue and Pass in Python

June 28, 2019

Read More
Business Days with Custom Holidays

May 30, 2019

Read More