sa2c / python-oop-novice

An introduction to object-oriented programming for Python users in research
https://sa2c.github.io/python-oop-novice/
Other
1 stars 5 forks source link

Function example in episode 2 #11

Closed markgdawson closed 3 years ago

markgdawson commented 3 years ago

https://edbennett.github.io/python-oop-novice/02-writing-classes/index.html

The text in episode 2 "However, what if we wanted to plot some of the curves in a thick blue line?" could be misinterpreted. Someone might potentially see this as saying that the same thing can't be achieved without classes.

I'd suggest moving the quadratic_plot function further down, and show the function equivalent to the "blue plotter" example (i.e. something in which you can set a plotter state once, and carry that plotter state around). Something like this potentially:

def quadratic_plotter(a, b, c, **kwargs):
        '''Plot the line a * x ** 2 + b * x + c and output to the screen.
        x runs between -10 and 10, with 1000 intermediary points.
        The line is plotted in the colour specified by color, and with width
        linewidth.'''

        fig, ax = subplots()
        x = linspace(-10, 10, 1000)
        ax.plot(x, a * x ** 2 + b * x + c,
                color=color, linewidth=linewidth)

blue_plotter = { "color" : color,
                          "linewidth" : linewidth}

quadratic_plotter(1, 2, 3, **blue_plotter)

Alternatives could create the dict in a constructor, or avoid using the **kwargs to pass the arguments (i.e. pass a "state" variable).

Places to move this to could be either just before Mutation revisited``or before theInitialising instances` title (after the excercises as a callout)