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
Uh oh!
There was an error while loading. Please reload this page.
-
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 theDepthDependentDiffusercomponent: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
classin 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 inDepthDependentDiffuseris automatically called andmy_gridis passed as an argument.Landlab components take advantage of an object-oriented programming technique called inheritance.
DepthDependentDiffusergives us an example: the line of code that defines the class reads:class DepthDependentDiffuser(Component). This syntax tells the Python interpreter that theDepthDependentDiffuserclass 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
Componentbase 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_infoitem are present, and throws an error if not.So far so good. But what a minute, what happens if
DepthDependentDiffuserdefines its own version of__init__()(as all components should)? In this case, the version of__init__()defined byDepthDependentDiffusertakes precedence; in object-oriented-speak, it overrides the base class function of the same name.But how then can we run both the
DepthDependentDiffuserversion 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 variablegridto 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