Fundamentals of Programming I Managing the Namespace

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
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 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
How to load a program file? Lisp programs in Allegro are saved under the file extension.cl. To load a file into the Lisp console, use the following: (load.
CHAPTER 6 Functions. Function Overview We’ve used built-in functions:  Examples:  print(“ABC”, x+10, sep=“:”)  round(x * 5, 2)  pygame.draw.circle(screen,
Computer Science 111 Fundamentals of Programming I Introduction to Programmer-Defined Classes.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”
Algorithmic Recursion. Recursion Alongside the algorithm, recursion is one of the most important and fundamental concepts in computer science as well.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Computer Science 111 Fundamentals of Programming I Dictionaries redux.
Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
Computer Science 111 Fundamentals of Programming I Default and Optional Parameters Higher-Order Functions.
Python Functions : chapter 3
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Scoping and Namespaces CSIS 1595: Fundamentals of Programming and Problem Solving 1.
JavaScript scope and closures
Review for Nested loops & Math class methods & User defined methods.
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.
Computer Science 101 Python Lists. Literals, Assignment, Comparisons, Concatenation Similar to the behavior of strings so far a = [1, 2, 3] b = range(1,
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
Input, Output and Variables GCSE Computer Science – Python.
CS314 – Section 5 Recitation 9
Fundamentals of Programming I Higher-Order Functions
Fundamentals of Programming I Default and Optional Parameters
Computer Programming Fundamentals
COMPSCI 107 Computer Science Fundamentals
Programming Fundamentals
Working with Dictionaries
Functions, locals, parameters, and separate compilation
A Lecture for the c++ Course
Fundamentals of Programming I Dictionaries redux
Chapter 6 Functions.
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Lecture 07 More Repetition Richard Gesick.
Object Oriented Programming
CHAPTER FOUR Functions.
Names, Scopes, and Bindings: Scopes
Python Primer 2: Functions and Control Flow
Haskell Strings and Tuples
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Chapter 8 Namespaces and Memory Realities
Fundamentals of Programming I Commonly Used Methods More Modeling
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
Computer Science 111 Fundamentals of Programming I
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Review for Final Exam.
G. Pullaiah College of Engineering and Technology
functions: argument, return value
15-110: Principles of Computing
CSE 231 Lab 4.
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
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.
CISC101 Reminders Assignment 3 due today.
 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.
Python fundamental.
Using Modules.
Presentation transcript:

Fundamentals of Programming I Managing the Namespace Computer Science 111 Fundamentals of Programming I Managing the Namespace

What Is the Namespace? The namespace of a program has to do with the arrangement of names in a program These names refer to modules, data values, functions, methods, and data types Best to minimize the number of names in a program, but still have to organize!

Example Names Modules – math, random, doctor, generator Data types – int, float, str, list Variables – average, qualifiers, replacements Functions – print, input, sum, math.sqrt, reply Methods – append, split, join, lower

Properties of a Name Binding time - the point at which a name is defined to have a value Scope - the area of program text in which a name has a particular value Lifetime - the span of time during which a name has a particular value

Binding Time of Module Variables qualifiers = ['Why do you say that ', 'You seem to think that ', 'Did I just hear you say that '] for q in qualifiers: print(q) def reply(sentence): return random.choice(qualifiers) + changePerson(sentence) A module variable is defined to have a value when the module is loaded into the Python interpreter Its binding time is thus at load time

Binding Times of Temps and Params def changePerson(sentence): oldlist = sentence.split() newlist = [] for word in oldlist: newlist.append(replacements.get(word, word)) return " ".join(newlist) changePerson(input("Enter a sentence:")) The binding time of a temporary variable is within a particular call of the function A parameter is bound to a value after the argument is evaluated during a particular call of the function

The Scope of a Name A name’s scope is the area of program text within which it has a particular value This value will be visible to some parts of a program but not to others Allows a single name to have different meanings in different contexts

The Scope of a Module Variable qualifiers = ['Why do you say that ', 'You seem to think that ', 'Did I just hear you say that '] for q in qualifiers: print(q) def reply(sentence): return random.choice(qualifiers) + changePerson(sentence) A module variable is visible throughout the module, even within a function definition Module variables may be reset with assignment, but not within a function definition

The Scope of a Parameter def reply(sentence): return random.choice(qualifiers) + changePerson(sentence) def changePerson(sentence): sentence = sentence.lower() oldlist = sentence.split() newlist = [] for word in oldlist: newlist.append(replacements.get(word, word)) return " ".join(newlist) A parameter is visible throughout its function definition but not outside of it Parameters may be reset with assignment

The Scope of a Temporary Variable def mySum(lyst): total = 0 for number in lyst: total += number return total def average(lyst): total = mySum(lyst) return total / len(lyst) A temporary variable is visible within its function definition but not outside of it Temporaries may be reset with assignment

The Scope of a Temporary Variable def changePerson(sentence): oldlist = sentence.split() newlist = [] for word in oldlist: newlist.append(replacements.get(word, word)) return " ".join(newlist) A temporary variable is visible throughout its function definition but not outside of it Temporaries may be reset with assignment

The Scope of a Loop Variable def changePerson(sentence): oldlist = sentence.split() newlist = [] for word in oldlist: newlist.append(replacements.get(word, word)) return " ".join(newlist) A loop variable is like a temporary variable but is visible only in the body of the loop Don’t ever reset loop variables with assignment!

How Scope Works x = "module" def f(): x = "temporary" print(x) print(x) # Outputs module f() # Outputs temporary A temporary variable and a module variable can have the same name, but they refer to different storage spaces containing possibly different values

Name Conflicts x = "module" def f(): x += "temporary" # Error: reference before assignment print(x) print(x) # Outputs module f() # Should output moduletemporary, but generates error Python thinks you’re referencing a temporary variable (the second x) before you define it

Limits on Module Scope history = [] for s in history: print(s) history += ["All computer scientists are cool!"] def reply(sentence): answer = random.choice(qualifiers) + changePerson(sentence) history = history + [sentence] # Error return answer Cannot assign a value to a module variable within a function definition Python thinks that history is a temporary variable within the reply function

Limits on Module Scope history = [] for s in history: print(s) def reply(sentence): answer = random.choice(qualifiers) + changePerson(sentence) history.append(sentence) # This is a better practice return answer Better to reference the variable’s value and mutate this value than to reset the variable itself

The Lifetime of a Variable The lifetime of a module variable is the span of time during which the module is active The lifetime of a parameter or a temporary variable is the span of time during which the enclosing function call is active

Optional, default, and keyword parameters For Friday Optional, default, and keyword parameters