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

cosc 2p91
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

COSC 2P91

Objects with class Week 8b

Brock University

Brock University (Week 8b) Objects with class 1 / 14

slide-2
SLIDE 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

slide-3
SLIDE 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

slide-4
SLIDE 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

slide-5
SLIDE 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

slide-6
SLIDE 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

  • perators.

init — Used for initialization del — Analogous to a destructor str — How object should be represented as a string getattr , setattr , and delattr — please don’t

Brock University (Week 8b) Objects with class 6 / 14

slide-7
SLIDE 7

Magic methods

Operators

add — + sub — − mul — ∗ mod — % pow — ∗∗ and — &

  • r

— | xor —ˆ eq , ne , gt , lt , ge , le lshift , rshift contains — in

Brock University (Week 8b) Objects with class 7 / 14

slide-8
SLIDE 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

slide-9
SLIDE 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

slide-10
SLIDE 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

slide-11
SLIDE 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

slide-12
SLIDE 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

slide-13
SLIDE 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

slide-14
SLIDE 14

Questions?

Comments?

Answers!

Brock University (Week 8b) Objects with class 14 / 14