Namespaces – Local and Global

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
Abstract Data Types and Encapsulation Concepts
Guide to Programming with Python Chapter Nine Working with/Creating Modules.
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.
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.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
CPS120: Introduction to Computer Science Functions.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Python Functions.
Introduction to Python By Neil Cook Twitter: njcuk Slides/Notes:
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
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 
Introduction to Computing Using Python Namespaces – Local and Global  The Purpose of Functions  Global versus Local Namespaces  The Program Stack 
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.
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)
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.
CS 1110 Introduction to Programming Spring 2017
Ruby Classes, Modules & Mixins
Chapter 6 Functions.
Topic: Functions – Part 2
Starting Out with Programming Logic & Design
Names, Binding, and Scope
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
Object-Oriented Programming
Chapter 2. Problem Solving and Software Engineering
CSE 231 Lab 4.
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
Functions, Procedures, and Abstraction
 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.
Introduction to Methods and Interfaces
The Linux Command Line Chapter 26
Presentation transcript:

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

The purpose of functions Introduction to Computing Using Python 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.

Local variables hide what goes on inside a function Introduction to Computing Using Python Enter this code in pythontutor and trace its execution. 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.

Function namespace Introduction to Computing Using Python 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? def g(n): print('Start g') n += 1 print('n = ', n) def f(n): print('Start f') g(n) n = 1 print('Outside a function, n = ',n) f(n)

Scope and global vs. local namespace Introduction to Computing Using Python 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.

Example: variable with local scope Introduction to Computing Using Python 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 >>> === RESTART ==== >>> >>> === RESTART ==== >>> f(3) = 18 a = 0 >>> === RESTART ==== >>> f(3) = 18 a shell b a Function call f(3) 3 6

Example: variable with global scope Introduction to Computing Using Python 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) = ', f(3)) print('a = ', a) # global a is still 0 >>> === RESTART ==== >>> >>> === RESTART ==== >>> f(3) = 0 >>> === RESTART ==== >>> f(3) = 0 a = 0 a shell b Function call f(3) 3

How Python evaluates names Introduction to Computing Using Python 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 printf 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? module builtins To find the value of a name, Python searches through namespaces in this order: First, the local (function) namespace Then the global namespace Finally the namespace of module builtins f a printf() global namespace b f() Function call f(3) 3

Modifying a global variable inside a function Introduction to Computing Using Python To modify a global variable inside a function, use the keyword 'global'. 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 b >>> === RESTART ==== >>> f(3) = 18 a = 6 shell Function call f(3) 3 6