Geography 465 Modules and Functions Writing Custom Functions

  • Slides: 11
Download presentation
Geography 465 Modules and Functions Writing Custom Functions

Geography 465 Modules and Functions Writing Custom Functions

Python Modules • Each Python script is a module • Modules can access other

Python Modules • Each Python script is a module • Modules can access other modules • Modules can use variables and/or constants • Modules can use functions

Writing a Function • What makes a good function – Single, clear objective –

Writing a Function • What makes a good function – Single, clear objective – Reusable by intent • Function syntax – Def defines the function – Return() sends result back to calling module – See example

Syntax Example My. Custom. Function. py Def <name> (arg 1, arg 2, …arg. N):

Syntax Example My. Custom. Function. py Def <name> (arg 1, arg 2, …arg. N): # your custom function code return <value>

Code example result of two numbers My. Functions. py def calculate(num 1, num 2,

Code example result of two numbers My. Functions. py def calculate(num 1, num 2, math. Op): result = eval(str(num 1) + math. Op + str(num 2)) return result Interactive Window >>> import My. Functions >>> My. Functions. calculate (3, 4, “+”) >>> 7

Using variables in functions • Assignment statement – Variables at module level are available

Using variables in functions • Assignment statement – Variables at module level are available to calling script – Variables within a function are local to the function Script 1. py Passing values to module function import New. York ny. Lat = New. York. lat ny. Long = New. York. long ny. Calc = New. York. calcgrowth (7000000, 0. 03) Print ny. Calc New. York. py lat = -73. 905 # Global long = 40. 708 # Global def calcgrowth(pop, rate): growth = pop * rate return growth

Importing a custom module • First time in Python – Finds the module –

Importing a custom module • First time in Python – Finds the module – Compiles the module – Runs the module • Subsequent times (same Python session) – Re-uses imported (compiled) module

Finding the module • Where does Python look to find module? • The search

Finding the module • Where does Python look to find module? • The search path hierarchy is: – Home directory of calling module – PYTHONPATH environment variable – Standard Python library directions – Contents of. pth (text) files

Adding a custom search path • Modify sys. path • Append location of your

Adding a custom search path • Modify sys. path • Append location of your custom modules • Last for duration of script • Resets to default when Python. Win closes Example. py import sys. path. append(“C: \My. Modules”) Print sys. path

Reloading a module single Python session • Module load only once Script 1. py

Reloading a module single Python session • Module load only once Script 1. py import New. York Access New. York. py lat = -73 long = 40 • To re-import, call reload() Interactive Window >>> reload(New. York) New. York. py Access modified lat = -73. 905 New. York. py long = 40. 708

Running the module • All statements execute from top to bottom – Statements do

Running the module • All statements execute from top to bottom – Statements do not exist until Python reaches and runs code (line by line interpret) – Code inside functions do not run until called Follow indent rules New. York. py lat = -73. 905 long = 40. 708 def calcgrowth (pop, rate): growth = pop * rate return growth