Academy for Academic Education

Python Classroom Series – 06/Jun/2021

Namespaces and Scope

  • Python programs have various namespaces – section within which a particular name is unique and unrelated to the same name in other namespaces.
  • Each function defines its own namespace.
  • Consider the following code
# variable fruit
fruit = 'mango'

def print_fruit():
    # variable my_fruit
    my_fruit = 'apple'
    print(fruit, my_fruit)

print_fruit()
print(fruit)
print(my_fruit)

  • If we try to execute the code we get the following error
  • The variable fruit inside the function is local to function
fruit = 'mango'


def print_fruit():
    fruit = 'apple'
    print(id(fruit))
    print(fruit)

print(id(fruit))
print(fruit)
print_fruit()
print(id(fruit))
print(fruit)
  • To access the global variable rather than local variable in the function, we need to be explicit and use the global keyword
fruit = 'mango'


def print_fruit():
    global fruit 
    fruit = 'apple'
    print(id(fruit))
    print(fruit)

print(id(fruit))
print(fruit)
print_fruit()
print(id(fruit))
print(fruit)
  • Now lets use two built in functions
    • locals() returns a dictionary of the contents of local namespace
    • globals() returns a dictionary of contents of the global namespace
  • Consider the following program
course = 'python'

def change_local():
    """
    This method will change the local value
    """
    course = 'django'
    print('locals: ',locals())

def print_globals():
    """
    This method is used to print globas
    """
    print('globals: ',globals())

change_local()
print_globals()

Naming Functions

  • In python it is not a good idea to create the functions with same name as built in functions Refer Here Preview
  • Using the same name as built ins will hide the built in functionality
def sum(*items):
    """
    This function calculates sum of items
    """
    print('using our sum method')
    result = 0
    for item in items:
        result += item
    return result

result = sum(1,2,3,4,5)
print(result)
# We want to use built in method
result = sum([1,2,3,4,5,6])
print(result)
  • If we execute this code we get the following result, the second sum with list in will not work as it is trying to call the built-in sum method which is hidden due to name conflict
  • To fix this situation we can use del keyword

Double Underscore Methods => dunder methods

  • Names that begin and end with two underscores (__) are reserved for use with in python
  • This naming pattern was chosen because it seemed unlikely to be used by application developers for their own variables
  • Refer Here for the examples used in the class

Exercise: Write a factorial implementation in python:

  • Write a python function to calculate factorial
def factorial(number):
    """
    This method calculates the factorial of the number
    """
    result = 1
    for index in range(1,number+1):
        result *= index
    return result

print(factorial(5))
  • Recursion is the approach where the function calls it self
def factorial(number):
    """
    This method calculates the factorial of the number
    """
    result = 1
    for index in range(1,number+1):
        result *= index
    return result

def fact(number):
    if number == 1:
        return 1
    else:
        return number * fact(number-1)

print(factorial(5))
print(fact(5))
Exit mobile version