Introduction to Computing Using Python Namespaces – Local and Global  The Purpose of Functions  Global versus Local Namespaces  The Program Stack 

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Perkovic, Chapter 7 Functions revisited Python Namespaces
Introduction to Computing Using Python Namespaces and Exceptions, revisited  Encapsulation in Functions  Global versus Local Namespaces  Exceptional.
CS0004: Introduction to Programming Repetition – Do Loops.
Guide to Programming with Python
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.
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
An Introduction to Python – Part IV Dr. Nancy Warter-Perez.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
Guide to Programming with Python Chapter Nine Working with/Creating Modules.
Introduction to Object-oriented programming and software development Lecture 1.
chap13 Chapter 13 Programming in the Large.
Chapter 6 Functions 1. Opening Problem 2 Find the sum of integers from 1 to 10, from 20 to 37, and from 35 to 49, respectively.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
1 Life Cycle of Software Specification Design –Risk Analysis –Verification Coding Testing –Refining –Production Maintenance.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
CPS120: Introduction to Computer Science Functions.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Python Functions.
1 CHAPTER 3 MODULAR PROGRAMMING. 2 Introduction  A library in C is a collection of general purpose and related functions.  2 types of libraries: Standard.
Introduction to Python By Neil Cook Twitter: njcuk Slides/Notes:
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
Lecture 05 Functions II, Storage Class, Scope, rand() METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet.
Introduction to Computing Using Python Namespaces – Local and Global  The Purpose of Functions  Global versus Local Namespaces  The Program Stack 
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
INLS 560 – F UNCTIONS Instructor: Jason Carter.
Chapter 12: Programming in the Large By: Suraya Alias 1-1.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
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.
6. FUNCTIONS Rocky K. C. Chang October 4, 2015 (Adapted from John Zelle’s slides)
CIT 590 Intro to Programming Lecture 2. Agenda ‘import’ Abstraction Functions Testing The concept of scope.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Exam #1 You will have exactly 30 Mins to complete the exam.
Ruby Classes, Modules & Mixins
Chapter 6 Functions.
Starting Out with Programming Logic & Design
Chapter 3 Simple Functions
Chapter 6 Methods: A Deeper Look
Teach A-Level Computer Science: Object-Oriented Programming in Python
The Linux Command Line Chapter 26
CS 1111 Introduction to Programming Fall 2018
Bryan Burlingame 13 February 2019
Topics Introduction to Functions Defining and Calling a Void Function
Namespaces – Local and Global
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
G. Pullaiah College of Engineering and Technology
Topics Introduction to Functions Defining and Calling a Function
Starting Out with Programming Logic & Design
CSE 231 Lab 4.
Namespaces – Local and Global
Functions, Procedures, and Abstraction
The Linux Command Line Chapter 26
Presentation transcript:

Introduction to Computing Using Python Namespaces – Local and Global  The Purpose of Functions  Global versus Local Namespaces  The Program Stack  How Python Evaluates Names

Introduction to Computing Using Python The purpose of functions Functions make it easier for us to code solutions to problems: Modularity: They allow us to break down complex problems that require complex programs into small, simple, self-contained pieces. Each small piece can be implemented, tested, and debugged independently. Abstraction: A function has a name that should clearly reflects what it does. That action can then be performed by calling the function by name, abstracting what the function does from how it does it. Code reuse: Code that may be used multiple times should be packaged in a function. The code only needs to be written once. And any time that it needs to be modified, extended or debugged, the changes need to be made only once.

Introduction to Computing Using Python Local variables hide what goes on inside a function def double(y): x = 2 y *= x print('inside double', 'x = ', x, 'y = ', y) x, y = 3, 4 print('outside double', 'x = ', x, 'y = ', y) double(y) print('after double', 'x = ', x, 'y = ', y) def double(y): x = 2 y *= x print('inside double', 'x = ', x, 'y = ', y) x, y = 3, 4 print('outside double', 'x = ', x, 'y = ', y) double(y) print('after double', 'x = ', x, 'y = ', y) First, x and y are created in the global frame. While double executes, local x and y are created with their own values. These local variables cease to exist when the function exits. Separating what happens inside a function from what happens outside is called encapsulation. Enter this code in pythontutor and trace its execution.

Introduction to Computing Using Python Function namespace def g(n): print('Start g') n += 1 print('n = ', n) def f(n): print('Start f') n += 1 print('n = ', n) g(n) n = 1 print('Outside a function, n = ',n) f(n) def g(n): print('Start g') n += 1 print('n = ', n) def f(n): print('Start f') n += 1 print('n = ', n) g(n) n = 1 print('Outside a function, n = ',n) f(n) Every execution of a function creates a local namespace that contains any local variables. Note that there are several active values of n, one in each namespace. How are all the namespaces managed by Python? Which line does Python return to?

Introduction to Computing Using Python Scope and global vs. local namespace Every function call has its own (local) namespace This namespace is where names defined during the execution of the function (e.g., local variables) live. This namespace comes into existence when the function is called. It goes out of existence when the function exits (returns). Every name in a Python program has a scope This applies to the name is of a variable, a function, a class, … Outside its scope, the name does not exist. Any reference to it will result in an error. Names created in the interpreter shell or in a module and outside of any function have global scope.

Introduction to Computing Using Python Example: variable with local scope b Function call f(3) >>> === RESTART ==== >>> >>> === RESTART ==== >>> a shell 0 0 def f(b): # f has global scope, b has local scope a = 6 # this a has scope local to this call of function f() return a*b # this a is the local a a = 0 # this a has global scope print('f(3) = ', f(3)) print('a = ', a) # global a is still 0 def f(b): # f has global scope, b has local scope a = 6 # this a has scope local to this call of function f() return a*b # this a is the local a a = 0 # this a has global scope print('f(3) = ', f(3)) print('a = ', a) # global a is still a 6 6 >>> === RESTART ==== >>> f(3) = 18 >>> === RESTART ==== >>> f(3) = 18 >>> === RESTART ==== >>> f(3) = 18 a = 0 >>> >>> === RESTART ==== >>> f(3) = 18 a = 0 >>>

def f(b): # f has global scope, b has local scope return a*b # this a is the global a a = 0 # this a has global scope print('f(3) = {}'.format(f(3))) print('a is {}'.format(a)) # global a is still 0 def f(b): # f has global scope, b has local scope return a*b # this a is the global a a = 0 # this a has global scope print('f(3) = {}'.format(f(3))) print('a is {}'.format(a)) # global a is still 0 def f(b): # f has global scope, b has local scope return a*b # this a is the global a a = 0 # this a has global scope print('f(3) = {}'.format(f(3))) print('a is {}'.format(a)) # global a is still 0 def f(b): # f has global scope, b has local scope return a*b # this a is the global a a = 0 # this a has global scope print('f(3) = {}'.format(f(3))) print('a is {}'.format(a)) # global a is still 0 def f(b): # f has global scope, b has local scope return a*b # this a is the global a a = 0 # this a has global scope print('f(3) = {}'.format(f(3))) print('a is {}'.format(a)) # global a is still 0 def f(b): # f has global scope, b has local scope return a*b # this a is the global a a = 0 # this a has global scope print('f(3) = {}'.format(f(3))) print('a is {}'.format(a)) # global a is still 0 def f(b): # f has global scope, b has local scope return a*b # this a is the global a a = 0 # this a has global scope print('f(3) = {}'.format(f(3))) print('a is {}'.format(a)) # global a is still 0 def f(b): # f has global scope, b has local scope return a*b # this a is the global a a = 0 # this a has global scope print('f(3) = {}'.format(f(3))) print('a is {}'.format(a)) # global a is still 0 Introduction to Computing Using Python Example: variable with global scope b Function call f(3) >>> === RESTART ==== >>> >>> === RESTART ==== >>> def f(b): # f has global scope, b has local scope return a*b # this a is the global a a = 0 # this a has global scope print('f(3) = ', f(3)) print('a = ', a) # global a is still 0 def f(b): # f has global scope, b has local scope return a*b # this a is the global a a = 0 # this a has global scope print('f(3) = ', f(3)) print('a = ', a) # global a is still 0 a shell >>> === RESTART ==== >>> f(3) = 0 >>> === RESTART ==== >>> f(3) = 0 >>> === RESTART ==== >>> f(3) = 0 a = 0 >>> >>> === RESTART ==== >>> f(3) = 0 a = 0 >>>

Introduction to Computing Using Python How Python evaluates names When there are duplicate uses of a name, how does Python decide which instance of the name (e.g., local or global) you are referring to? To find the value of a name, Python searches through namespaces in this order: 1.First, the local (function) namespace 2.Then the global namespace 3.Finally the namespace of module builtins b Function call f(3) 3 3 a global namespace printf module builtins def f(b): # f has global scope, b has local scope return a*b # this a is the global a a = 0 # this a has global scope print('f(3) = ', f(3)) print('a = ', a) # global a is still 0 def f(b): # f has global scope, b has local scope return a*b # this a is the global a a = 0 # this a has global scope print('f(3) = ', f(3)) print('a = ', a) # global a is still f f() printf()

Introduction to Computing Using Python Modifying a global variable inside a function b Function call f(3) def f(b): global a # all references to a in f() are to the global a a = 6 # global a is changed return a*b # this a is the global a a = 0 # this a has global scope print('f(3) = ', f(3)) print('a = ', a) # global a has been changed to 6 def f(b): global a # all references to a in f() are to the global a a = 6 # global a is changed return a*b # this a is the global a a = 0 # this a has global scope print('f(3) = ', f(3)) print('a = ', a) # global a has been changed to 6 a shell >>> === RESTART ==== >>> f(3) = 18 a = 6 >>> >>> === RESTART ==== >>> f(3) = 18 a = 6 >>> 6 6 To modify a global variable inside a function, use the keyword 'global'.