Python Modules.

Slides:



Advertisements
Similar presentations
15. Python - Modules A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand.
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1.
Chapter Modules CSC1310 Fall Modules Modules Modules are the highest level program organization unit, usually correspond to source files and.
Setting the PYTHONPATH PYTHONPATH is where Python looks for modules it is told to import List of paths Add new path to the end with: setenv PYTHONPATH.
This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Perkovic, Chapter 7 Functions revisited Python Namespaces
An Introduction to Python – Part IV Dr. Nancy Warter-Perez May 19, 2005.
An Introduction to Python – Part IV Dr. Nancy Warter-Perez June 23, 2005.
VBA Modules, Functions, Variables, and Constants
Concepts when Python retrieves a variable’s value Namespaces – Namespaces store information about identifiers and the values to which they are bound –
Geography 465 Modules and Functions Writing Custom Functions.
An Introduction to Python – Part IV Dr. Nancy Warter-Perez.
1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.
Guide to Programming with Python Chapter Nine Working with/Creating Modules.
Ganga 3 CLIP Tutorial Jakub T. Moscicki ARDA/LHCb Ganga Tutorial, April 2005.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Python Modules An Introduction. Introduction A module is a file containing Python definitions and statements. The file name is the module name with the.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
This Week The string type Modules print statement Writing programs if statements (time permitting) The boolean type (time permitting)
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
By James Braunsberg. What are Modules? Modules are files containing Python definitions and statements (ex. name.py) A module’s definitions can be imported.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 5 Function Interfaces 4/18/09 Python Mini-Course: Day 2 - Lesson 5 1.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS Jehan-François Pâris
Overview Intro to functions What are functions? Why use functions? Defining functions Calling functions Documenting functions Top-down design Variable.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 5: Functions Xiang Lian The University of Texas – Pan American Edinburg, TX
Introduction to Computing Using Python Namespaces – Local and Global  The Purpose of Functions  Global versus Local Namespaces  The Program Stack 
Function and Function call Functions name programs Functions can be defined: def myFunction( ): function body (indented) Functions can be called: myFunction(
Language Find the latest version of this document at
Chapter 15. Modules Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome.
Modules. Modules Modules are the highest level program organization unit, usually correspond to source files and serve as libraries of tools. Each file.
LECTURE 2 Python Basics. MODULES So, we just put together our first real Python program. Let’s say we store this program in a file called fib.py. We have.
Juancho Datu. What is a Module? File containing Python definitions and statements with the suffix ‘.py’ in the current directory For example file name:
Chapter 15 - C++ As A "Better C"
Lesson 06: Functions Class Participation: Class Chat:
Exam #1 You will have exactly 30 Mins to complete the exam.
Python’s Modules Noah Black.
G. Pullaiah College of Engineering and Technology
Lecture 2 Python Basics.
IST256 : Applications Programming for Information Systems
Python’s Modules by E. Esin GOKGOZ.
JavaScript: Functions
JavaScript Functions.
Data Analysis using Python-I
CHAPTER FOUR Functions.
exa.im/stempy16.files - Session 12 Python Camp
Functions, Procedures, and Abstraction
Lesson 06: Functions Class Chat: Attendance: Participation
Namespaces – Local and Global
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
Geography 465 Managing Custom Python Script Tools
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Structured Programming
Loops and Simple Functions
Python Modules.
What is a Function? Takes one or more arguments, or perhaps none at all Performs an operation Returns one or more values, or perhaps none at all Similar.
Namespaces – Local and Global
Functions, Procedures, and Abstraction
Review We've seen that a module is a file that can contain classes as well as its own variables. We've seen that you need to import it to access the code,
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
SPL – PS1 Introduction to C++.
Presentation transcript:

Python Modules

Modules in a Nutshell Modules are text files you can import to the Python interpreter in order to reuse previously written code. A module has its own global and local scopes to prevent interference with user defined names. A package is a collection of modules, grouped for organization and easy access.

Module Example $ vim mymodule.py # a simple module def square(x): “”” Returns the square of its parameter. return x * x

Using a module >>> import mymodule >>> y = mymodule.square(2) >>>y 4 >>>mymodule.square(y) 16

Call by Value >>> import mymodule >>> x = 7 >>> mymodule.square(x) 49 >>> x 7

Accessing the module variables You can access the global variables of a module using dot notation. >>> x = somemodule.somevariable Global variables in a module will not otherwise effect another module, or user defined components.

from/import Statements You can import specific names from a module with a from/import statement. >>> from mymodule import square >>> square(3) 9 >>> from mymodule import square, cube >>> cube(2) 8 >>> from mymodule import *

The Importance of Returning Python modules don’t really handle function calls by reference. Programmers have to work around this with return statements. >>> x = 2 >>> x = mymodule.square(2) >>> x 4

Modules as Scripts $ vim mymodule.py def square(x): print x * x if __name__ == "__main__": import sys square(int(sys.argv[1])) ~ $ python mymodule.py 2 4

.pyc Files On a successful import, a modulename.pyc file is generated. This file contains the bytecode from the last time the modulename.py file was imported. The interpreter checks the date modified before using the .pyc file, there is no danger of using an older version. These files only speed up the loading of a module, not the execution.

dir() dir() is useful for checking what modules you’ve imported. ['__builtins__', '__doc__', '__name__', '__package__'] >>> import mymodule >>> from mymodule import square ['__builtins__', '__doc__', '__name__', '__package__', 'mymodule', 'square']

Standard Modules import first checks the current directory for the specified module. The interpreter then follows a default path like /usr/local/lib/python, or the PYTHONPATH EV. To change the path, import “sys”, one of Python’s standard modules. >>> import sys >>> sys.path.append(‘/home/jm4564/cs_265’)

A Package Example math/ __init__.py logarithms.py circles.py statistics/ factorial.py …

Importing Packages Packages can be imported in as large, or as small, portions as you require. >>> import math.circles >>> math.circles.area(5) 78.53981634 >>> from math.circles import area >>> area(3) 28.27433388

Questions?

Source Python v2.6.4 documentation >> The Python Tutorial >> Modules http://docs.python.org/tutorial/modules.html