Return to Video

Scope Intro - Design of Computer Programs

  • 0:00 - 0:03
    The purpose of this segment is to talk about scope which is a concept that we've been
  • 0:03 - 0:06
    using in the course but haven't actually talked about specifically
  • 0:06 - 0:10
    And so if you've been getting confused by decorators which decorate decorators
  • 0:10 - 0:15
    or functions, calling functions, return functions, and so on, this video is for you.
  • 0:15 - 0:17
    So when we talk about a variable scope,
  • 0:17 - 0:20
    we're talking about the region in the program in which we can access that variable.
  • 0:20 - 0:24
    This is a concept that can go from being very simple to quite difficult.
  • 0:24 - 0:27
    So let's start with something basic and build from there.
  • 0:27 - 0:32
    Here we've got some very simple code, x=2, y=3, print the sum and of course, we get 5.
  • 0:32 - 0:35
    In this example, x and y are what's called global variables
  • 0:35 - 0:39
    and then later what we call the global environment which I'm going to represent with this box
  • 0:39 - 0:43
    and in this environment, x is bound to the value 2 and y is bound to the value 3.
  • 0:43 - 0:47
    The global environment is also where all our primitive operators are bound.
  • 0:47 - 0:50
    So for example the plus sign is bound to some object which carries out addition
  • 0:50 - 0:53
    and we won't go into too much detail about what's going on there.
  • 0:53 - 0:57
    So let's make things a bit more interesting--so here I've just defined a function called add_nums
  • 0:57 - 1:02
    and it returns x+y and when I run it, I get 11.
  • 1:02 - 1:06
    So what's happened here is that our function has created a new scope
  • 1:06 - 1:12
    and this scope is local to the function and so what happened when we defined add_nums
  • 1:12 - 1:15
    was we made this binding between this name and the function object.
  • 1:15 - 1:20
    Now that's fine but as soon as we call add_nums by saying
  • 1:20 - 1:24
    print the result print add_nums, we created a new environment
  • 1:24 - 1:28
    Now I'm going to call this environment add_nums, and in this environment, we made an assignment.
  • 1:28 - 1:31
    There are two assignment from x to 5 and from y to 6
  • 1:31 - 1:37
    and when we call add_nums, Python prefers to choose variables within its local scope.
  • 1:37 - 1:41
    If it can do that, it then moves up following this pointer
  • 1:41 - 1:44
    to what's called its enclosing environment since this add_nums environment
  • 1:44 - 1:48
    lives inside of this global environment and lives there.
  • 1:48 - 1:51
    If after following all these paths up into the global environment, it still doesn't find
  • 1:51 - 1:55
    the variable it's looking for that's when we get an error-- specifically a name error.
Title:
Scope Intro - Design of Computer Programs
Description:

more » « less
Video Language:
English
Team:
Udacity
Project:
CS212 - Design of Computer Programs
Duration:
01:56

English subtitles

Revisions Compare revisions