computer science 2p05
play

COMPUTER SCIENCE 2P05 Programming Languages Data Abstraction and - PowerPoint PPT Presentation

COMPUTER SCIENCE 2P05 Programming Languages Data Abstraction and Encapsulation Brock University Brock University (Data Abstraction and Encapsulation)Programming Languages 1 / 25 Abstraction Recall that weve covered numerous forms of


  1. COMPUTER SCIENCE 2P05 Programming Languages Data Abstraction and Encapsulation Brock University Brock University (Data Abstraction and Encapsulation)Programming Languages 1 / 25

  2. Abstraction Recall that we’ve covered numerous forms of abstraction, both procedural and data. For a problem of any significant size, it becomes intractable to consider “all things” simultaneously Sometimes we write functions, methods, etc. to simplify how we can write algorithms, and sometimes we want to use a single object as a substitute for a more complicated arrangement of information Brock University (Data Abstraction and Encapsulation)Programming Languages 2 / 25

  3. Abstract Data Types ADTs Remember all the way back to first-year? ADTs? Would anyone care to take a stab at defining what an ADT is ? Brock University (Data Abstraction and Encapsulation)Programming Languages 3 / 25

  4. Data types that are abstracted! (Helpful, right?) Yes, an ADT is abstract. ...and? Well, let’s look at what we know a good ADT will typically include: A well-defined interface ◮ e.g. is a stack without a push or pop really a stack? Information-hiding ◮ We’d rather use getters and setters (accessors and updaters), rather than directly manipulating information ⋆ Both because it reduces the chances of unexpected side effects, and because we can control what is/isn’t allowed ◮ We remember how we achieve this in Java, right? Often the ability to have multiple implementations ◮ Though this largely relates back to the first point Brock University (Data Abstraction and Encapsulation)Programming Languages 4 / 25

  5. Reminder on encapsulation (Before we get to the fun stuff) As a quick reminder: when we talk about encapsulation , we’re talking about bundling up state information, and the operations you can perform on that information, together This is, of course, closely related to closures ◮ You can look at a closure as being an example of encapsulation ◮ For this lecture, we’ll be looking at a different example: defining operators to operate on records to create ADTs Brock University (Data Abstraction and Encapsulation)Programming Languages 5 / 25

  6. Sample data structure We’re going to need a sample ADT. Something we can use for a couple examples Let’s go with a basic group-queue : We’ll add records in groups ◮ These groups will remain effectively atomic for their time within the GQ We can also remove a group, which follows FIFO compared to the other groups We can traverse the GQ ◮ This does not mean the GQ itself will be printing! Traversal has many applications; not all of which are printing! We can concatenate two GQs into a larger single one We can retrieve the i th group We can ask if an entire group (group member?) is within the GQ Seems like a good start. We can revise it if necessary. Brock University (Data Abstraction and Encapsulation)Programming Languages 6 / 25

  7. Our Group Queue in Java How would we go about making such a thing in Java? First, we’d probably want to write an interface , right? ◮ That interface would include abstract method headers ◮ We’d define English-ish methods to correspond to the operators We’d provide one or more concrete implementations ◮ These would need to satisfy the interface’s promises ◮ We’d need to pick constructors that make sense ⋆ We might even use a little constructor chaining We’d probably also define some exceptions , but... next (lecture) week Considering this is the kind of thing we’ve been doing since 1P03, we can skip actually making this, right? Brock University (Data Abstraction and Encapsulation)Programming Languages 7 / 25

  8. Okay, then how about another language? Like, random suggestion... Python? Can we write an interface in Python? What about an abstract class? Let’s answer the latter first: Yes, we can... sort of We could expliclty create a class and provide dummy implementations that gripe if you try to instantiate them ◮ Think: something roughly analogous to the typical remove() method of an Iterator in Java Alternatively, there’s an abc (Abstract Base Class) module for formalizing this Also, this is one of those you need to do it differently between Python 2 and 3 things More reading here: ◮ https://docs.python.org/2.7/library/abc.html ◮ http://masnun.rocks/2017/04/15/interfaces-in-python-protocols-and-abcs/ (We could’ve just as easily answered this for C++, which does have e.g. pure virtual functions , but we’d be here all day if we wanted to cover everything ) Brock University (Data Abstraction and Encapsulation)Programming Languages 8 / 25

  9. Yeah, but what about interfaces? Well, that’s the thing. What is the primary difference between an interface and an abstract class? Perhaps that an abstract class is directly involved in implementation? Maybe an interface is purely-abstract, and, e.g. defines an ADT? (You could get lots of answers for this question!) Perhaps this means that what most matters for an interface is that it defines the type according to how you use it ? Brock University (Data Abstraction and Encapsulation)Programming Languages 9 / 25

  10. Let’s test this theory! Let’s see how quickly we can define the following (let’s say in Java, not that it should matter much): A stack A queue An Iterator An Iterable Brock University (Data Abstraction and Encapsulation)Programming Languages 10 / 25

  11. What does this mean? Quack quack! This term has come up before, but Python uses Duck-Typing If it walks like a duck, and quacks like a duck, then it’s a duck How does this help us? First, we need to actually write our data structure. To the code! Brock University (Data Abstraction and Encapsulation)Programming Languages 11 / 25

  12. That example is incomplete! This is true! We haven’t learned enough to finish it yet! Let’s look at an easy one: being able to access the i th element It may not seem like it, but this is actually an operator Specifically, the [] operator Recall the [] we’ve seen the most: slices for lists (or substrings for strings) Depending on the exact usage and number of parameters, there are several related operators Let’s try it out? Brock University (Data Abstraction and Encapsulation)Programming Languages 12 / 25

  13. Let’s expand a bit more? What else can we do with the [] ? Let’s see! Brock University (Data Abstraction and Encapsulation)Programming Languages 13 / 25

  14. Just a bit more before the real fun! Before we get to the real fun, let’s look at checking membership , and also try using a more conventional operator? Brock University (Data Abstraction and Encapsulation)Programming Languages 14 / 25

  15. How did that concatenation work? We just relied on the concatenation provided by the list type. Don’t re-invent the wheel if you don’t need to However, how confident are we that it really worked? Put another way, do we remember the difference between a deep copy and a shallow copy ? Brock University (Data Abstraction and Encapsulation)Programming Languages 15 / 25

  16. Traversal time! Okay, so it’s finally time for some iteration! And... what defines something as iterable ? IT’S A DUCK! ◮ Wait, not quite ◮ It’s iterable if it can give an iterator (and can, if desired, be its own iterator) ◮ How/when? It has an iter that’s called by the iter() function ⋆ If it’s its own iterator, it’ll just return self Okay... then what’s an iterator ? In Python 2: It has a next function, called by next() In Python 3: It has next , still called by next() Let’s try it out? Brock University (Data Abstraction and Encapsulation)Programming Languages 16 / 25

  17. It broke Indeed it did. That’s because, traditionally, iterators keep going until they break. That’s not a bug; it’s a feature When we’re done iterating, we just throw (well, raise ) a StopIteration exception! Let’s see what we mean? Brock University (Data Abstraction and Encapsulation)Programming Languages 17 / 25

  18. Generators Okay, so then, what’s a generator ? Remember the list comprehensions we’ve briefly touched on a couple of times? ◮ If not, we can write a quick example The problem with them is they create the entire list; they allocate everything in advance This is fine if we want all the values simultaneously, but if we only need one at a time, that can be a memory hog (as well as front-loading our computation time when we could be spreading it out) A generator is an object that can provide each item as you need it . It behaves like an iterator (and actually creates one for you). Let’s first look at a generator expression , as it’s very close to the list comprehension. Brock University (Data Abstraction and Encapsulation)Programming Languages 18 / 25

  19. Are those the only generators? Nope. There’s also a big one we hinted at very early on. Remember how we briefly discussed coroutining last week? For the subset of problems where we just want one thing to keep producing elements, and another to keep consuming them, with control being handed off back and forth (it comes up more than you’d think), we have a reasonable alternative: yield The yield statement turns the current function into a generator ◮ Whenever a yield is encountered, execution within that function is suspended until something pulls out the next token via the generator’s next ◮ Since it creates an iterator for you, it will raise a StopIteration if/when it exhausts Perhaps two quick examples? Brock University (Data Abstraction and Encapsulation)Programming Languages 19 / 25

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend