Exceptions and Making our own classes
description
Transcript of Exceptions and Making our own classes
Slide 1
Exceptions and Making our own classesMany slides from this class are from those provided with the text, created by Terry Scott, University of Northern Colorado. However, I will annotate most of them and add others as needed.2Exceptions: How to Deal with Error Situationsnumber = 0while not 1 0: #don't scale if point is at originself.scale(1/mag)31Improved Point Class (continued)#allow print to be able to print a point object.def __str__(self):return ''
#using __str__ methodnew = Point(3, 5)print new#output
32Improved Point Class (continued)Can not use to initialize an object. point = #this is an errorCan overload most operators so that they have a new meaning when used with new objects.An example is + operator when used with int and float does addition. When used with str it does concatenation (sticks the two strings together).33Improved Point Class (continued)#overloading operators: + overloadingdef __add__(other):return Point(self._x +other._x, self._y+other._y
#using the __add__ methodnew = Point(3, 5)old = Point(4, 7)total = new + oldprint total#output
34PolymorphismOperator may do a different operation depending on the type that is passed into the operator.Multiplication operator: int or float multiply each component by the value, point do a dot product.isinstance(variable, Type) returns True if variable is of type Type.35Polymorphism#if val is an int or float it does the if code #if a Point it does the elif code.def __mul__(self, val):if isinstance(val, (int, float)):#performs regular multiplication operation.return Point(self._x*val, self._y*val)elif isinstance(val, Point):#performs dot product operation.return self._x*val._x + self._y*val._y Spot CheckDo exercise 6.4 in the text. Use the Discussion Board in Blackboard to discuss the question with others in the class.37Television ClassCreate a user class to emulate the way television controls work.General principlesOn-off and mute are both toggle switchesAll controls only work when the TV is on.Volume control goes from1 to 10 inclusive.Channels range from 2 99 inclusive. It wraps around.Can change channel by entering a channel number.38Television Class#initializes television objectclass Television:def __init__(self):self._powerOn = Falseself.muted = Falseself._volume = 5self._channel = 2self._prevChan = 239Television Class Diagram
40Television Class (continued)#Clicking flips if on then off and off then ondef togglePower(self):self._powerOn = not self._powerOn
#Clicking flips between muted and unmuted.def toggleMute(self):if self._powerOn:self._muted = not self._muted41Television Class (continued)#volume can range from 1 upto including 10def volumeUp(self):if self._powerOn:if self._volume < 10: self._volume += 1self._muted = Falsereturn self_volume #volume is #displayed on tv when it is changed. 42Television Class (continued)#channel increases by one and wraps back to 2def channelUp(self): if self._powerOn:self._prevChan = self._channelif self._channel == 99: self._channel = 2else: self._channel += 1return self._channel43Television Class (continued)volumeDown is similar to volumeUp just replace test self._volume < 10 with self._volume > 1 and replace self._volume += 1 with self._volume -= 1
channelDown is similar to channelUp just replace test self._channel == 99 with self._channel == 2 and replace self._channel += 1 with self._channel -= 1. 44Television Class (continued)#Channel is set to number.def setChannel(self, number): if self._powerOn: if 2