python programming - iv. program components (functions, classes, modules, packages)

Post on 10-May-2015

2.321 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

Feel free to download the material for offline viewing later, better images' resolutions, and crispier fonts.

TRANSCRIPT

PYTHON PROGRAMMING

Engr. Ranel O. Padon

IV. Program Components

PYTHON PROGRAMMING TOPICS

I • Introduction to Python Programming

II • Python Basics

III • Controlling the Program Flow

IV • Program Components: Functions, Classes, Modules, and Packages

V • Sequences (List and Tuples), and Dictionaries

VI • Object-Based Programming: Classes and Objects

VII • Customizing Classes and Operator Overloading

VIII • Object-Oriented Programming: Inheritance and Polymorphism

IX • Randomization Algorithms

X • Exception Handling and Assertions

XI • String Manipulation and Regular Expressions

XII • File Handling and Processing

XIII • GUI Programming Using Tkinter

THE TARGET SCENARIO

THE BUILDING BLOCKS

DIVISION OF LABOR

DIVISION OF LABOR

THE ASSEMBLY LINE

DIVIDE-AND-CONQUER

every problem can be broken down into smaller/more

manageable sub-problems

DIVIDE-AND-CONQUER ALGORITHM

most computer programs that solve real-world problems are

complex/large

the best way to develop and maintain a large program is to

construct it from smaller pieces or components

PYTHON PROGRAM COMPONENTS

functions

classes

modules

collection of functions & classes

packages

collection of modules

Package

Module

PYTHON PROGRAM COMPONENTS

Function

Class

FUNCTIONS

collection or block of statements that you can execute

whenever and wherever you want in the program

FUNCTIONS

FUNCTIONS

WHY FUNCTIONS?

avoids duplicating code snippets

saves typing

easier to change the program later

PYTHON MODULES

groups related functions & classes

PYTHON MODULES

mathematical calculations

string manipulations

character manipulations

web programming

graphics programming

PYTHON MODULES

STANDARD LIBRARY

collection of Python modules

found in C:\Python27\Lib folder

PYTHON MODULES

STANDARD LIBRARY

* found in C:\Python27\Lib

PYTHON MODULES

STANDARD LIBRARY

familiarize yourself with the Standard Library

don’t reinvent the wheel

PYTHON PACKAGES

groups related modules

code calling in several locations

prevents name collision

PYTHON PACKAGES (site-packages)

PYTHON PACKAGE (numpy)

PYTHON SUB-PACKAGE (polynomial)

showing the Modules of the polynomial package.

PYTHON MODULE (polynomial.py)

showing a part of the content of the polynomial module

PYTHON FUNCTIONS

* groups related modules

* code calling in several locations

* prevents name collision

PYTHON FUNCTIONS

* groups related modules

* code calling in several locations

* prevents name collision

PYTHON FUNCTIONS (math MODULE)

* groups related modules

* code calling in several locations

* prevents name collision

PYTHON FUNCTIONS (math MODULE)

PYTHON FUNCTIONS (DEFINITION)

PYTHON FUNCTIONS (DEFINITION)

PYTHON FUNCTIONS (DEFINITION)

PYTHON FUNCTIONS (return KEYWORD)

PYTHON FUNCTIONS (return KEYWORD)

PYTHON FUNCTIONS (return TUPLES)

PYTHON FUNCTIONS

def sumDiff(x, y):

return (x+y), (x-y)

sum, diff = sumDiff(2, 3)

print sum

print diff

PYTHON FUNCTIONS

PYTHON FUNCTIONS (random MODULE)

PYTHON FUNCTIONS (random MODULE)

PYTHON FUNCTIONS (random MODULE)

VARIABLE SCOPE

all variables in a program may not be accessible at all locations

in that program

the scope of a variable determines the portion of the program

where you can access a particular variable

VARIABLE SCOPE

variables that are defined inside a function body have a local

scope, and those defined outside have a global scope

inside a function, a local variable takes precedence over a

global variable of the same name

possible workaround:

change the variable names to avoid collision

VARIABLE SCOPE

VARIABLE SCOPE

PYTHON FUNCTIONS (ARGUMENTS)

You can call a function by using the

following types of formal arguments:

Required Arguments

Default Arguments

Keyword Arguments

Variable-Length Arguments

PYTHON FUNCTIONS (PARAMS vs ARGS)

Function Parameters

Function Arguments

PYTHON FUNCTIONS (ARGUMENTS)

Required/Mandatory Arguments

passed to a function in correct positional order

PYTHON FUNCTIONS (ARGUMENTS)

Keyword Arguments

the caller identifies the arguments by the parameter name as

keywords, with/without regard to positional order

PYTHON FUNCTIONS (ARGUMENTS)

Default/Optional Arguments

assumes a default value if a value is not provided in the function

call for that argument.

PYTHON FUNCTIONS (ARGUMENTS)

Variable-Length Arguments

can handle no-argument, 1-argument, or many-arguments function

calls

PYTHON FUNCTIONS (ARGUMENTS)

Combining the Argument Types

NAMESPACES

refers to the current snapshot of loaded

names/variables/identifiers/folders

functions must be loaded into the memory before you could call

them, especially when calling external functions/libraries

NAMESPACES (Importing a Package)

current snapshot of the default

and imported namespaces

NAMESPACES (Importing Packages)

NAMESPACES (Importing Functions)

NAMESPACES (Importing Functions)

NAMESPACES (Built-In Functions)

NAMESPACES (Importing All Functions)

NAMESPACES (Importing All Functions)

NAME BINDING

they are used for better readability, faster coding,

or simply just for convenience

NAME BINDING

NAME BINDING

NAME BINDING

NAME BINDING

HANDLING MODULES

HANDLING MODULES (Error?)

HANDLING MODULES (Debug)

HANDLING MODULES (Correct Import)

HANDLING MODULES (Correct Import)

HANDLING MODULES (Alternative)

HANDLING MODULES (Will Work)

HANDLING MODULES (Will Not Work)

It could not locate the sum() function.

HANDLING PACKAGES

The __init__.py files are required to make Python treat the

directories as containing packages

HANDLING PACKAGES (The GE file)

The geodetic_engineering.py file/module, located in the

engineering and diliman parent folders/packages.

HANDLING PACKAGES (Will Not Work)

The demo.py file importing the geodetic_engineering.py file/module.

HANDLING PACKAGES (Will Work)

The demo.py file importing the geodetic_engineering.py file/module.

HANDLING PACKAGES (Will Work)

The demo.py file importing the geodetic_engineering.py file/module.

HANDLING PACKAGES (Will Work)

The demo.py file importing the geodetic_engineering.py file/module.

HANDLING PACKAGES (Will Work)

The demo.py file importing the geodetic_engineering.py file/module.

HANDLING PACKAGES (Summary)

IMPORTATION INVOCATION

import p1.p2.m p1.p2.m.f1()

from p1.p2 import m m.f1()

from p1.p2.m import f1 f1()

Where p means package, m means module, f means a function/class.

PRACTICE EXERCISE 1

Compute the factorial of a number n:

• n is a number inputted by the user

• make a factorial function and call it to solve

the factorial of n

PRACTICE EXERCISE 2

Compute the sum of a number range, say, from a to b, inclusive:

• a, b are numbers inputted by the user

• make a sum_range(a, b) function and call it to solve the

sum of all numbers from a to b, including a and b.

Divide-and-Conquer is Powerful!

REFERENCES

Deitel, Deitel, Liperi, and Wiedermann - Python: How to Program (2001).

Disclaimer: Most of the images/information used here have no proper source

citation, and I do not claim ownership of these either. I don’t want to reinvent the

wheel, and I just want to reuse and reintegrate materials that I think are useful or

cool, then present them in another light, form, or perspective. Moreover, the

images/information here are mainly used for illustration/educational purposes

only, in the spirit of openness of data, spreading light, and empowering people

with knowledge.

top related