Functions and Modules
CSE304- Python Programming with Web Frameworks
Text Book: Michael Urban, Joel Murach. Murach’s Python Programming, Mike Murach & Associates,
First Indian Reprint, 2017.
Functions
A function is a unit of code that performs a task
It is used to divide the code for a program into manageable chunks
This makes it easier to maintain, test, and debug our code
Besides that, functions can be used by more than one program
The Syntax for defining a function
Functions without argument
7/31/2024 2
Cont.
Functions with one argument
Functions with two arguments and returns a value
7/31/2024 3
Default Arguments
• Assigning default values to one or more arguments
• Name of the argument = value
• All default arguments must be placed at the end of argument list
Syntax:
def fname(argument_list, default_arg = value):
statements …
Eg.
def f1(a, b, c=3):
return a+b+c
Call of function:
f1(10, 20, 30)
f1(1, 2)
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 4
Named Arguments
• In function call, passing values using name
• Name of the argument = value
• When passing values as named arguments, the order of arguments need not be in order.
• Syntax:
fname(argument_name = value)
• Eg.
def f1(a, b, c):
return a+b+c
• Call of function:
f1(a=10, c=20, b=30)
f1(b=1, c=2, a=3)
f1(b=8, a=20, c=12)
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 5
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 6
Example
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 7
Example
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 8
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 9
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 10
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 11
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 12
Namespace
• Name (also called identifier) is simply a name given to objects
• Everything in Python is an object
• Name is a way to access the underlying object
• For example, when we do the assignment a = 2, 2 is an object stored in memory and ‘a’ is the name we
associate with it.
• We can get the address (in RAM) of some object through the built-in function id
• This is efficient as Python does not have to create a new duplicate object
• This dynamic nature of name binding makes Python powerful; a name could refer to any type of object.
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 13
Python Variable Scope
Python Variable Scope
• Although there are various unique namespaces defined, we may not be able to access all of them from
every part of the program.
• The concept of scope comes into play.
• A scope is the portion of a program from where a namespace can be accessed directly without any
prefix
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 14
Scope Vs Namespace
Python Scope
• The scope refers to the region in the program code where variables and names are
accessible
• It also determines the visibility and lifetime of a variable in the program code
• The names or objects which are accessible are called in-scope
• The names or objects which are not accessible are called out-of-scope
• The Python scope concept follows the LEGB (Local, Enclosing, Global and built-in) rule.
Python Scope Vs Namespace
• The scope of a variables and objects are related to the concept of the namespace
• The scope determines the visibility of the names and objects in the program
• A namespace is a collection of names
• In python, scopes are implemented as dictionaries that maps names to objects. These dictionaries are called
namespaces
• The dictionaries have the names as key and the objects as value
• A strings, lists, functions, etc everything in Python is an object
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 15
Built-in namespace
The built-in namespace contains the names of all of Python’s built-in objects. These are available at all times when Python is running.
The Python interpreter creates the built-in namespace when it starts up. This namespace remains in existence until the interpreter
terminates.
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 16
global namespace
The global namespace contains any names defined at the level of the main program
Python creates the global namespace when the main program body starts, and it remains in existence until
the interpreter terminates
This may not be the only global namespace that exists
The interpreter also creates a global namespace for any module that our program loads with the import
statement
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 17
Local Scope
The Variables which are defined in the function are a local scope of the variable. These variables are defined in the
function body
num is local to the function. When we use the num variable value in the function before declaring it locally, it raises an
error
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 18
When we want to change the value of the global variable inside the function global keyword is used
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 19
NonLocal or Enclosing Scope
• Nonlocal Variable is the variable that is defined in the nested function
• It means the variable can be neither in the local scope nor in the global scope
• To create a nonlocal variable nonlocal keyword is used
• In the following code, we created an outer function, and there is a nested function inner()
• In the scope of outer() function inner() function is defined
• If we change the nonlocal variable as defined in the inner() function, then changes are reflected in the outer function
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 20
NonLocal or Enclosing Scope
• Nonlocal Variable is the variable that is defined in the nested function
• It means the variable can be neither in the local scope nor in the global scope
• To create a nonlocal variable nonlocal keyword is used
• In the following code, we created an outer function, and there is a nested function inner()
• In the scope of outer() function inner() function is defined
• If we change the nonlocal variable as defined in the inner() function, then changes are reflected in the outer function
7/31/2024 21
module
7/31/2024 22
modules
23
7/31/2024
modules
• A module is a file with python code
• The code can be in the form of variables, functions, or class defined
• The filename becomes the module name
• For example, if our filename is [Link], the module name will be simple
• With module functionality, we can break our code into different files instead of writing everything inside
one file
• The function or variables present inside the file can be used in another file by importing the module
• Within a module, the module’s name (as a string) is available as the value of the global variable
__name__
• This does not add the names of the functions defined in moduleexample directly to the current namespace
• It only adds the module name,using the module name we can access the functions:
7/31/2024 24
Modules
• A module is a file that stores reusable code
• For importing a module
• Syntax:
• import module_name [as namespace]
• from module_name import fun_1 [, fun_2] … [as name]
• from module_name import *
• Namespaces support modularity by preventing naming conflicts. For instance, the functions
[Link]() and [Link]() are distinguished by their namespaces
• When importing a module, it is stored in a namespace specified. If namespace is not given, it will
be imported into a namespace same as the name of the module
• When one or more functions are imported from a module it is stored in a namespace specified. If
namespace is not given, it will be imported to the global namespace
• If two modules having same function name imported in to the global namespace, a name collision
occurs.
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 25
Variant of the import statement that imports names from a module directly into the importing module’s namespace
Example:
This does not introduce the module name from which the imports are taken in the local namespace
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 26
Import module from different directory using the sys module
• We can use [Link] to add the path of the new different
folder (the folder from where we want to import the modules)
to the system path so that Python can also look for the
module in that directory if it doesn’t find the module in its
current directory
• As [Link] falls under the list type class so, we can easily
use the insert method to add the folder path
Ref: [Link]
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 27
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 28
Few standard modules
• math for mathematical operations
• random for generating random nos.
• decimal for working with decimal nos.
• csv for working with csv files
• pickle for persistent data storage
• tkinter for building GUI applications
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 29
If you intend to use a function often you can assign it to a local name:
• Each module has its own private namespace, which is used as the global namespace
by all functions defined in the module
• Author of a module can use global variables in the module without worrying about
accidental clashes with a user’s global variables
• We can touch a module’s global variables with the same notation used to refer to its
functions, [Link]
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 30
_ _name_ _ (A Special variable) in Python
• Since there is no main() function in Python, when the command to run a python program is given to the
interpreter, the code that is at level 0 indentation is to be executed
• However, before doing that, it will define a few special variables
• __name__ is one such special variable
• If the source file is executed as the main program, the interpreter sets the __name__ variable to have a
value “__main__”
• If this file is being imported from another module, __name__ will be set to the module’s name
• __name__ is a built-in variable which evaluates to the name of the current module
• Thus it can be used to check whether the current script is being run on its own or being imported
somewhere else by combining it with if statement
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 31
OUTPUT:
OUTPUT: ?
OUTPUT:
7/31/2024 ? 32
OUTPUT:
OUTPUT:
OUTPUT:
7/31/2024 33
OUTPUT: ?
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 34
OUTPUT:
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 35
OUTPUT:
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 36
OUTPUT:
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 37
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 38
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 39
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 40
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 41
If module2 is executed first
If Module1 is changed as follows
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 42
If module2 is
executed first
If Module1 is changed as follows
7/31/2024 CSE304 - Python Programming with Web Frameworks [Link] 43