[Go to site: main page, start]

0% found this document useful (0 votes)
13 views15 pages

Understanding Python Modules and Scope

The document explains Python modules, which are files containing functions and variables that can be imported into applications. It covers how to create, use, and rename modules, as well as built-in modules and the use of the dir() function. Additionally, it discusses variable scope, including local, global, and nonlocal scopes, along with how to manage them using keywords.

Uploaded by

sundymaung
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views15 pages

Understanding Python Modules and Scope

The document explains Python modules, which are files containing functions and variables that can be imported into applications. It covers how to create, use, and rename modules, as well as built-in modules and the use of the dir() function. Additionally, it discusses variable scope, including local, global, and nonlocal scopes, along with how to manage them using keywords.

Uploaded by

sundymaung
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Modules

What is a Module?

Let’s consider a module to be the same as a code library.


It is just a file containing a set of functions you want to include in your
application.

To create a module just save the code you want in a file with the file extension
.py. Save this code in a file named [Link]

def greeting(name):
print("Hello, " + name)
Use a Module

Now we can use the module we just created, by using the import statement:

import mymodule

[Link]("Jonathan")
Variables in Module
The module can contain functions, as already described, but also variables of
all types (arrays, dictionaries, objects etc):
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}

Import the above module, and access the person1 dictionary:

import mymodule

a = mymodule.person1["age"]
print(a)
Naming a Module
You can name the module file whatever you like, but it must have the file
extension .py

Re-naming a Module
You can create an alias when you import a module, by using the as keyword:

import mymodule as mx

a = mx.person1["age"]
print(a)
Built-in Modules
There are several built-in modules in Python, which you can import whenever
you like.

import platform

x = [Link]()
print(x)
Using the dir() Function
There is a built-in function to list all the function names (or variable names) in
a module. The dir() function:

import platform

x = dir(platform)
print(x)
Import From Module
You can choose to import only parts from a module, by using the from
keyword.
def greeting(name):
print("Hello, " + name)

person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}

from mymodule import person1

print (person1["age"])
Python Scope
What is a Scope?
A variable is only available from inside the region it is created. This is called
scope.

Local Scope
A variable created inside a function belongs to the local scope of that
function, and can only be used inside that function.

def myfunc():
x = 300
print(x)

myfunc()
Function Inside Function
The variable x is not available outside the function, but it is available for any
function inside the function.

def myfunc():
x = 300
def myinnerfunc():
print(x)
myinnerfunc()

myfunc()
Global Scope
A variable created in the main body of the Python code is a global variable
and belongs to the global scope.
Global variables are available from within any scope, global and local.

x = 300

def myfunc():
print(x)

myfunc()

print(x)
Global Keyword
If you need to create a global variable, but are stuck in the local scope, you
can use the global keyword.
The global keyword makes the variable global.

def myfunc():
global x
x = 300

myfunc()

print(x)
Nonlocal Keyword
The nonlocal keyword is used to work with variables inside nested functions.
The nonlocal keyword makes the variable belong to the outer function.

def myfunc1():
x = "Jane"
def myfunc2():
nonlocal x
x = "hello"
myfunc2()
return x

print(myfunc1())
You’re now expert about
Python Modules and Scope

You might also like