Lecture 6 - 2/19/11 Lecture 6 ECE 364: Software Tools...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
2/19/11 1 ECE 364: Software Tools Laboratory Lecture 6 February 14, 2011 1 Lecture 6 § Namespaces and Scope § Functions § Modules § Exceptions § Menus § Code examples available at § ~ee364/examples/LEC-06 § Also downloadable from Blackboard 2 Namespaces § A namespace is “a mapping from names to objects” § e.g. “ x ” maps to the value 5 or l A z maps to [1, 2, 3] § Python creates namespaces at different times § The built-in namespace is created when Python starts and is never deleted § __builtin__ § A namespace is created for a function when the function is called and deleted when the function returns 3 Scope § A scope is "a textual region of a Python program where a namespace is directly accessible" § A new local namespace is created whenever a function is called. § Contains the names of the function parameters and local variables. § Python resolves identifiers by searching their scope in the following order: § 1. Local namespace § 2. Global namespace within the module § 3. “Built-in" namespace § 4. If the name can not be resolved a NameError exception is raised 4
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Scope (2) 5 Y=0 # X and Y are part of the __global__ namespace X=0 def foo(): X = 10 # X and Y are part of a new namespace Y = 40 # __global__.foo and are distinct from print X, Y # X and Y in the global namespace def bar(): X = 30 # Python will search for names within print X, Y # the local namespace upwards to the # __global__ namespace. Y is found __global__ print X, Y # prints 0 0 foo() # prints 10 40 print X, Y # prints 0 0 bar() # prints 30 0 print X, Y # prints 0 0 Comparison: Scope in C § Scope in C is defined by blocks § Created using curly braces void foo(void *bar) { int i = 0; while (bar[i] != ` \0 ` ) { i++; } return i; } 6 Global Variables § To write to a variable that is in any namespace other than the current, local namespace you must declare it as global in the current namespace: >>> def DoIt(): >>> global A >>> A = 25 >>> A = 23 >>> DoIt() >>> print A 25 Avoid using global, it causes more bad than good. 7 Functions § Recall defining a new function: def FuncName(a, b): c = a + b return c § May have zero or more parameters § Returning a value is optional. § A function that does not explicitly return a
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/26/2011 for the course ECE 364 taught by Professor Niklaselmqvist during the Fall '10 term at Purdue University-West Lafayette.

Page1 / 8

Lecture 6 - 2/19/11 Lecture 6 ECE 364: Software Tools...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online