Docsity

Programacion I: funciones II en Python (transparencias) - Business Analytics, Diapositivas de Programación Informática

Vista previa parcial del texto

¡Descarga Programacion I: funciones II en Python (transparencias) - Business Analytics y más Diapositivas en PDF de Programación Informática solo en Docsity!

Last updated on 28/07/2020 09:

Business Analytics

PROGRAMACIÓN I

FUNCTIONS II

‣ Execution frames and namespaces

‣ Scope of variables

‣ Local and global keywords

FUNCTIONS II

1. Title

An assignment statement creates a symbolic name that you can use to reference an object.

  • The statement x = 'foo' creates a symbolic name x that refers to the string object 'foo’.

A namespace is a collection of currently defined symbolic names along with information about the

object that each name references.

  • You can think of a namespace as a dictionary:
    • keys are the object names
    • values are the objects themselves.
  • Each key-value pair maps a name to its corresponding object. Assignments and namespaces

1. Title

  • When you do an assignment, like a = 1, you’re changing/updating a namespace.
  • When you make a reference, like print(a), Python looks through a list of namespaces to try and

find one with the name as a key.

We are going to see the structure of namespaces and how a reference is resolved.

Resolving

‣ Execution frames and namespaces

‣ Scope of variables

‣ Local and global keywords

FUNCTIONS II

1. Title

  • A scope defines which namespaces will be looked in and in what order.
  • The scope of any reference always starts in the local namespace, and moves outwards until it

reaches the module's global namespace, before moving on to the built-ins (the namespace that

references Python's predefined functions and constants, like range and print), which is the end of

the line.

Scope

1. Resolving variables enclosing namespace Title A_CONSTANT = 3 def outer(): def inner(): a_variable = 7 print(a_variable) another_variable = 2 print(another_variable) inner() outer() print(A_CONSTANT)

A_CONSTANT = 3

def outer(): def inner(): print(another_variable) another_variable = 2 print(another_variable) inner() outer()

A_CONSTANT = 3

def outer(): def inner(): print(A_CONSTANT) another_variable = 2 print(another_variable) inner() outer()

Variable resolved in the namespace of an enclosing function.

The scope of another_variable includes the namespace of outer, as enclosing function of inner,

so print(another_variable) in inner works

1. Resolving variables in the global namespace Title A_CONSTANT = 3 def outer(): def inner(): a_variable = 7 print(a_variable) another_variable = 2 print(another_variable) inner() outer() print(A_CONSTANT)

A_CONSTANT = 3

def outer(): def inner(): print(another_variable) another_variable = 2 print(another_variable) inner() outer()

A_CONSTANT = 3

def outer(): def inner(): print(A_CONSTANT) another_variable = 2 print(another_variable) inner() outer()

Variable resolved in the global namespace (of the module where it is called).

The A_CONSTANT is defined in the global namespace, which is reachable by any function in the

module, in particular the function of inner, so print(A_CONSTANT) in inner works

1. Title

Whenever you define a function, you create a new namespace and a new scope.

  • The namespace is the new, local hash of names.
  • The scope is the implied chain of namespaces that starts at the new namespace, then works its

way through any outer namespaces (outer scopes), up to the global namespace (the global scope),

and on to the built-ins.

Resolution, summary

1. Scope, summary Title

Scope: defines the parts of the program where names can be used without using any prefix

§ A local scope: innermost scope that contains a list of local names available in the

current function.

§ A scope of all the enclosing functions. The search for a name starts from the nearest

enclosing scope and moves outwards.

§ A module level scope that contains all the global names from the current module.

§ The outermost scope that contains a list of all the built-in names. This scope is

searched last to find the name that you referenced.

‣ Execution frames and namespaces

‣ Scope of variables

‣ Local and global keywords

FUNCTIONS II

1. Title

  • Global variable: defined at module level (in the global namespace). Can be used by any function

in the module.

  • Local variable: defined in a function used inside the function
  • Non local variable: defined in a function, used in a nested function. Global, local and non local variables

1. Shadowing Title _# This generates an Exception, because global_var is defined in the

function and therefore it is considered local,

but in print, it is not defined

It is, in general, bad practice to "shadow" with local variables

variables names from outer scope_

def my_funciton(): local_var = 2 print(global_var, local_var) global_var = 3 global_var = 1 my_funciton() Exception (error)

1. Shadowing (II) Title _# This does not generate an error, but, again, local_var

in inner is shadowing the same name in an outer scope

Inner does not change outer's local_var_

def outer(): def inner(): local_var = 7 print(local_var) local_var = 2 print(local_var) inner() print(local_var) outer()