Why do Landlab components often use "super()"? #10
gregtucker
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Someone asked me a question about Landlab components earlier today, and it occurred to me that others probably have the same question, so I'm answering it here on the CSDMS User Forum.
If you browse through the source code of a typical Landlab component, you'll often see that one of the first lines in the
__init__()
function looks something like:super().__init__(grid)
For example, this is the first line in the
__init__()
function of theDepthDependentDiffuser
component:https://github.com/landlab/landlab/blob/master/landlab/components/depth_dependent_diffusion/hillslope_depth_dependent_linear_flux.py
What does this line do?
First, some key pieces of background information:
A Landlab component is implemented as a
class
in Python. A class is a kind of blueprint for creating a custom data type, called an object, that can include both data and functions (aka methods)In a Python class, the name
__init__
is used for a special function: one that is called as soon as an object is created (or "instantiated" in object-oriented vernacular). For example, when the line of codediffuser = DepthDependentDiffuser(my_grid)
is run, the__init__()
function inDepthDependentDiffuser
is automatically called andmy_grid
is passed as an argument.Landlab components take advantage of an object-oriented programming technique called inheritance.
DepthDependentDiffuser
gives us an example: the line of code that defines the class reads:class DepthDependentDiffuser(Component)
. This syntax tells the Python interpreter that theDepthDependentDiffuser
class should inherit all the properties of another class calledComponent
. What'sComponent
? It is a class defined in the Landlab codebase that is designed to serve as a base class for all Landlab components. When a class inherits fromComponent
, it will automatically include all the properties and methods ofComponent
. You can see its source code here: https://github.com/landlab/landlab/blob/master/landlab/core/model_component.pyThe
Component
base class has an__init__()
function that does some useful things. For example, it checks to make sure all the required input fields listed in the header_info
item are present, and throws an error if not.So far so good. But what a minute, what happens if
DepthDependentDiffuser
defines its own version of__init__()
(as all components should)? In this case, the version of__init__()
defined byDepthDependentDiffuser
takes precedence; in object-oriented-speak, it overrides the base class function of the same name.But how then can we run both the
DepthDependentDiffuser
version and the base class (Component
) version of__init__()
? That's wheresuper()
comes in: it says "get me the base class" (which can be thought of as the "super" class). So the linesuper().__init__(grid)
says "go to the base class, run it's__init__()
function, and pass the variablegrid
to that function."To learn more, there are plenty of resources online about classes and
super()
; here's one:https://realpython.com/python-super/
Beta Was this translation helpful? Give feedback.
All reactions