cosc 2p91
play

COSC 2P91 Objects with class Week 8b Brock University Brock - PowerPoint PPT Presentation

COSC 2P91 Objects with class Week 8b Brock University Brock University (Week 8b) Objects with class 1 / 14 Before we begin Wanna see a neat trick? Remember the conundrum of how to swap the contents of two variables without using a third


  1. COSC 2P91 Objects with class Week 8b Brock University Brock University (Week 8b) Objects with class 1 / 14

  2. Before we begin Wanna see a neat trick? Remember the conundrum of how to swap the contents of two variables without using a third variable? Well, first, do we remember how to do it in C? Anyhoo, how do you think you’d do it in Python? Brock University (Week 8b) Objects with class 2 / 14

  3. Classes Python classes are handled slightly differently from Java classes. Because you don’t declare variables before use, you also don’t declare instance variables* Methods must receive a reference to the instance of the class as the first parameter Operators can be overridden by defining special methods Inheritance will look a bit different Example time? Brock University (Week 8b) Objects with class 3 / 14

  4. Classes Accessing members Just as with Java, the way we access an instance’s members (variables or methods) is with the standard ‘dot syntax’ — e.g. b.tweet() . If we want, though, we can also add additional members on the fly How would we know if an ostensible object reference can really have its members accessed (as opposed to really being None)? ◮ Just use our old friend, the short-circuit and ◮ x and x.dealie Note: though peculiar, you can access a class instance’s functions manually ◮ Bird.tweet(b) Brock University (Week 8b) Objects with class 4 / 14

  5. Classes New-style classes and inheritance You can explicitly declare a ‘base type’ for a class. In general, this means you can define classes as explicit subtypes of object (instead of of instance ). But you can also make it a subtype of another class you’ve already created, allowing for inheritance In either case, simply follow a convention of class MyClass(ParentA,ParentB): Brock University (Week 8b) Objects with class 5 / 14

  6. Magic methods Magic methods are special methods of specific names that, when provided, can override default behaviours for a class. They can be as simple as behaving like a constructor, or as powerful as redefining the basic operators. — Used for initialization init del — Analogous to a destructor — How object should be represented as a string str getattr , setattr , and delattr — please don’t Brock University (Week 8b) Objects with class 6 / 14

  7. Magic methods Operators — + add sub — − — ∗ mul mod — % — ∗∗ pow and — & — | or —ˆ xor eq , ne , gt , lt , ge , le lshift , rshift contains — in Brock University (Week 8b) Objects with class 7 / 14

  8. Magic methods More operators pos — + — − neg invert — ˜ getitem , setitem , and delitem getslice , setslice , and delslice Of course, there are even more, but this is more than a good start. Brock University (Week 8b) Objects with class 8 / 14

  9. Classes Properties Simply using instance variables is generally fine, but not entirely safe It may be preferable to rely on getter and setter functions instead The variable will start with two underscores You can set it as a property in one of two ways: ◮ Assign a class variable, invoking property with the getter and setter functions ◮ Precede the getter and setter with the @property tag Note that these can be used to include guards, or ensure that actions are triggered when instance variables are accessed/changed. Let’s just go to an example, and also throw in a static function Brock University (Week 8b) Objects with class 9 / 14

  10. Documentation So, by now, I’ve probably used help a lot, right? It’s pretty handy how it knows how to use everything. In fact, it’s roughly comparable to Javadoc, isn’t it? That’s not an accident Documentation in Python is handled via doc strings . The first string literal at the start of a class or method definition becomeos the doc string Since they’re the only way to do multiline strings, triple-quotes are popular for this If provided, you can also find the doc in doc Brock University (Week 8b) Objects with class 10 / 14

  11. Exceptions As with Java, Python’s exceptions are a way for a signal to be sent from a deeper level within the code, breaking execution and jumping back out to a higher level to handle it. If an exception is caught, it may be handled and proceed or exit gracefully. If not, trouble. Generally, put the code that might raise an exception in a try: block Catch an exception with except You can use else for code that will only run if no exception is raised You can have code that will certainly run at the end with finally You can choose to receive additional information, beyond the exception itself To define your own custom exceptions, just extend the Exception class. https://docs.python.org/2/library/exceptions.html Brock University (Week 8b) Objects with class 11 / 14

  12. Larger modules Now that we’ve learned how to use classes, it should probably be obvious that we won’t really want to have a module constrained to a single file. And, in fact, there’s no reason we need to. You can, for example, have an entire folder become a module Simply include an init .py file ◮ It explains what to do when the folder is imported ◮ You’ll need to define what ’*’ (all) is, though Brock University (Week 8b) Objects with class 12 / 14

  13. If we have time... Want to see something neat? stuff=[6,4,3,6,7] for i,x in enumerate(stuff): print str(i)+" ⁀ "+str(x) It returns tuples of both indices and contents; good for when you need both Also, while we’re at it, if we have time, we should go back to our generators/list comprehensions. Brock University (Week 8b) Objects with class 13 / 14

  14. Questions? Comments? Answers! Brock University (Week 8b) Objects with class 14 / 14

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