Decorated Wrappers – Design of Computer Programs

Decorated Wrappers – Design of Computer Programs


So here’s an idea. Let’s get rid of this line, and instead, let’s declare that n_ary is a decorator. We’ll write a definition of what it means to be a decorator in terms of updating wrappers. Then we’ll be done, and we’ve done it once and for all. We can apply it to n_ary, and we can apply it to any other decorator that we define. This is starting to get a little bit confusing because here we’re trying to define decorator, and decorator is a decorator. Have we gone too far into recursion? Is that going to bottom out? Let’s draw some pictures and try to make sense of it. So we’ve defined n_ary, and we’ve declared that as being a decorator, and that’s the same as saying n_ary=decorator of n_ary. Then we’ve used n_ary as a decorator. We’ve defined sequence to be an n_ary function. That’s the same as saying sequence=n_ary of sequence. Now we wanted to make sure that there’s an update so that the documentation and the name of sequence gets copied over. We want to take it from this function, pass it over to this function because that’s the one we’re going to keep. While we’re at it, we might as well do it for n_ary as well. We want to have the name of n_ary be n_ary and not something arbitrary that came out of decorator. So we’ve got 2 updates that we want to do for the function that we decorated and for the decorator itself. Now let’s see if we can write decorator so that it does those 2 updates. So let’s define decorator. It takes an argument (d), which is a function. Then we’ll call the function we’re going to return _d, and that takes a function as input. So it returns the update wrapper from applying the decorator to the function and copying over onto that decorated function, the contents of the original function’s documentation and name, and then we also want to update the wrapper for the decorator itself. So from (d) the decorated function, we want to copy that over into _d and then return _d. Now which update is which? Well, this one here is the update of _d with d, and this one is the update of the decorated function from the function. So here we’re saying the new n_ary that we’re defining gets the name from the old n_ary, the name in the documentation string, and here we’re saying the new sequence, the new n_ary sequence, gets its name from the old sequence. Here’s what it all looks like. If you didn’t quite follow that the first time, don’t worry about it. This is probably the most confusing thing in the entire class because we’ve got functions pointing to other functions, pointing to other functions. Try to follow the pictures. If you can’t follow the pictures, that’s okay. Just type it into the interpreter. Put these definitions in. Decorate some functions. Decorate some n_ary functions. Take a look at them and see how it works.

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2019 Geted Tabs Online. All rights reserved.