temp_f?list of values:model = fit_model(train)fig = plot_scatterplot(data)save_image(img, path)Functions help to break up your code into small, reusable “modules.”
These modules can be composed together:
Programming is less about tricky logic problems, more about writing abstractions and composing them together.
class WeatherStation:
def __init__(self, temps_f): # Default initialization method
self.temps_f = temps_f # an "attribute"
def convert_f_to_c(temp_f): # A "method"
return 5/9 * (temp_f - 32)
def convert_temps_f_to_c(self): # Another "method"
return [self.convert_f_to_c(x) for x in self.temps_f]Object a name: WeatherStation.A class is a general purpose construct, like a function.
We have to initialize our class with some data:
Here, station is an instance of the class WeatherStation.
Then we can use the methods of the class for this instance:
pandas DataFrameDescriber class: here.
Classes can be extended to represent different objects objects with the same interface.
Here, the WeatherStation has a general purpose method get_temperatures_c which should always return the temperature in Celsius.
We could create two child classes which inherit the WeatherStation interface.
Assuming an AmericanWeatherStation is always initialized with temps in Fahrenheit:
Assuming a EuropeanWeatherStation is always initialized with temps in Celsius:
Inheritance gives a common interface.
Now, I can write a function that consumes any WeatherStation object.
Using built-in functions (and the standard library)
Writing your own functions
Composing functions
Core concepts:
Writing custom classes
Initializing classes
Class inheritance
Tutorial #2 includes the following code:
This requires actually breaking our code into different scripts (.py files).
We can’t do this because we are still using Colab.
.py files this afternoon!Default arguments
Keyword arguments
Artist, Song, Album, and Playlist.