Linewidth.''' def polynomial ( x ): '''For a given x and list of n+1 coefficients , linewidth ) class PolynomialPlotter ( FunctionPlotter ): def plot ( self, coefficients ): '''Given the list of coefficients , Linewidth.''' fig, ax = subplots () x = linspace ( self. x_max = x_max def plot ( self, function ): '''Plot a function of a single argument. SolutionĬlass FunctionPlotter : def _init_ ( self, color = 'red', linewidth = 1, x_min =- 10, x_max = 10 ): assert is_color_like ( color ) self. Taking this a step further, write a more general FunctionPlotterĬlass, and adjust PolynomialPlotter to be a subclass of it. linewidth ) class QuadraticPlotter ( PolynomialPlotter ): def plot ( self, a, b, c ): super (). polynomial ( x, coefficients ), color = self. The line is plotted in the colour specified by color, and with width ''' result = 0 for coefficient in coefficients : result = result * x + coefficient return result def plot ( self, coefficients ): '''Given the list of coefficients , Returns the polynomial f(x) = ax^n + bx^(n-1) + cx^(n-2) +. x_max = x_max def polynomial ( self, x, coefficients ): '''For a given x and list of n+1 coefficients , The Triangle class would then become:įrom numpy import linspace from matplotlib.pyplot import subplots from lors import is_color_like class PolynomialPlotter : def _init_ ( self, color = 'red', linewidth = 1, x_min =- 10, x_max = 10 ): assert is_color_like ( color ) self. Way up the tree until the first class which has a method of theĬorrect name, and calls that. When weĬall a method of the super() object, Python automatically works its Gives us access to the superclass (and any superclasses further up theĬhain), without having to refer to any one of them by name. What we can do instead is make use of the super() function. Once, even though the class knows what its parent class is. Since we have to specify the name of the Polygon class more than We could use Polygon._init_, but this still has some repetition, If we can, we’d like to remove thisīy using the equivalent method from the Polygon class. The line self.side_lengths = side_lengths appears in the One niggling issue is that we are still repeating ourselves a little Have to recreate this, since we can use it as-is. Method, which is defined on the Polygon parent class-we don’t Next, we’ve defined a new method area, which is onlyĪvailable on the Triangle class. This means that only the _init_ methodįrom the Triangle class is called, and not the one in the PolygonĬlass. _init_ method of the Polygon parent class, since we now need toĬheck that the sides that the shape is being given form a triangle,Īnd not some other shape. \(\theta_\) is an example of a bad value, so this would want to Say, forĮxample, we have a function to convert temperatures from degrees Celsius to We can also use this to define our own exceptions. We saw earlier that ValueError is a subclass of Exception, and that thisĬan be used to handle both specific and more general exceptions in a The child class, subclass, or derived class. Superclass, or base class, while the new class inheriting from it is called The class that is being inherited from is called the parent class, A class that inheritsįrom a second class automatically gains all of the second’s attributes and Mechanism to avoid this in the form of inheritance. Thankfully, Python (and most other languages that have classes) give us a QuinticPlotter as well? This repetitive code would quickly start to build Of the code common to both of them? What if we wanted a QuarticPlotter and a However, what happens if we want to write two classes thatĭo similar but distinct things? For example, if we wanted to write aĬubicPlotter as well as our QuadraticPlotter, would we need to repeat all We have talked about using classes as a way to reduce repetition in the Be able to use inheritance to construct parent-child relationships between classesīe able to override methods on child classes, and refer back to the parent class’s implementations
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |