Return to Video

Scope and Memo - Design of Computer Programs

  • 0:00 - 0:04
    Okay, so this is fairly basic so far. What is all this have to do with CS212.
  • 0:04 - 0:09
    So in Unit 3, we introduce this memo decorator and the question that's still popping multiple times
  • 0:09 - 0:14
    in the forums whereas how can a memorize function or specifically, how can multiple memorize functions
  • 0:14 - 0:18
    all maintained their own separate cache when it appears that cache is defined here
  • 0:18 - 0:19
    and so let's talk about that.
  • 0:19 - 0:24
    And as a reminder, memo gets past the function f and returns a new function which we've called _f
  • 0:24 - 0:29
    that contains this cache and this cache basically prevents us from repeating calculations,
  • 0:29 - 0:32
    and so the best way to learn anything in programming is still do some experiments.
  • 0:32 - 0:36
    So I've written a couple of simple functions here filled in square.
  • 0:36 - 0:39
    Just as a reminder, let's take a look at what our main space
  • 0:39 - 0:41
    or our global environment looks like like now.
  • 0:41 - 0:46
    And now what we've done is we head to the global environment bindings between memo
  • 0:46 - 0:49
    and its respective object and likewise, we filled in square.
  • 0:49 - 0:54
    Know that we haven't created any new frames yet, any new environments because that doesn't happen
  • 0:54 - 0:57
    until we actually call a function and we haven't called any of these functions.
  • 0:57 - 1:01
    All right, so what I'm going to do now is I'm going to memorize these functions by adding
  • 1:01 - 1:06
    the memo decorator and remember, this is the same--adding this decorator and memo
  • 1:06 - 1:09
    is the same as saying fib equals memo of fib.
  • 1:09 - 1:13
    This is just some nice syntactic sugar to put on the element, looks like a little nicer.
  • 1:13 - 1:18
    And so before we start calling fib and square, I'm going to introduce this print statement into memo
  • 1:18 - 1:22
    so that we can try to get an idea of what's going on.
  • 1:22 - 1:27
    So here I'm going to print fib(4) and we can see the cache getting updated as we would expect.
  • 1:27 - 1:33
    By the end, we will get the result 5 and also have this dictionary contain fib(3), (2), (1) and (0).
  • 1:33 - 1:36
    So the cache is behaving as expected.
  • 1:36 - 1:41
    Let's see what happens if I run now while making to print commands to fib(4)
  • 1:41 - 1:42
    and this is interesting.
  • 1:42 - 1:47
    The memorization now occurs in the first print statement in the first time we make these calls.
  • 1:47 - 1:50
    By turning at the second call to fib(4), we already have our cache.
  • 1:50 - 1:55
    We don't have to do all of these calls to fib. We just get 5. Great!
  • 1:55 - 2:02
    Now, the big question--is this cache the same as the cache in the square function up here.
  • 2:02 - 2:04
    You've probably have a good guess at the answer but let's just confirm.
  • 2:04 - 2:10
    So same thing except when we call square twice and you can see, when I call square the first time,
  • 2:10 - 2:14
    it's not telling me that the cache is the cache associated with fib--
  • 2:14 - 2:20
    telling me it's empty as we expect, and when I call the second time, of course, we're all set.
  • 2:20 - 2:23
    We have this mapping 4 to 16. Good. Now, how does this happen?
  • 2:23 - 2:29
    Well, the trick is--a new environment isn't created until we call a function.
  • 2:29 - 2:33
    So what happened here is we called memo and we set at_memo--so when we said at_memo
  • 2:33 - 2:38
    of fib, we created a new environment what I'm going to call memo.
  • 2:38 - 2:42
    In this environment, there was a binding from fib to the associated object.
  • 2:42 - 2:46
    Actually, since I didn't do the update wrapper step that we did in class,
  • 2:46 - 2:50
    this function name is actually still called _f, but again no separate environment
  • 2:50 - 2:53
    for _f yet because we didn't call it yet.
  • 2:53 - 2:57
    Likewise, when I plugged the decorator to square, a new environment was created with its own _f
  • 2:57 - 3:00
    though this one points to the square function object.
  • 3:00 - 3:03
    I almost forgot the important part--both of these have their own cache.
  • 3:03 - 3:09
    Now, when I called fib, that's when this environment was created, and when we called fib,
  • 3:09 - 3:11
    that's when we created this new environment.
  • 3:11 - 3:14
    And we can see that when fib goes looking for cache, if it looks in its local environment
  • 3:14 - 3:19
    and doesn't find one, it moves one step of the hierarchy, finds the cache associated
  • 3:19 - 3:24
    with this call to memo, uses that, and doesn't care at all about this cache over here.
  • 3:24 - 3:28
    Just because these have the same name, they live in totally separate environments,
  • 3:28 - 3:34
    and since fib only has access to everything over here and square could only access
  • 3:34 - 3:39
    everything over here, there was no correlation, no conflict at all that these both have the name cache
  • 3:39 - 3:42
    and we can use the memo decorator exactly the way we want to.
  • 3:42 - 3:45
    Thank you Python. Now, this is a lot to cover in a few minutes.
  • 3:45 - 3:50
    If you're still confused, feel free to check out the links below on scope and closure,
  • 3:50 - 3:52
    which is the topic that we didn't get to today--good luck.
タイトル:
Scope and Memo - Design of Computer Programs
概説:

more » « less
Video Language:
English
Team:
Udacity
プロジェクト:
CS212 - Design of Computer Programs
Duration:
03:53

English subtitles

改訂 Compare revisions