Skip to content

Modflow objects are difficult to subclass #984

@CesarRodriguezPereira

Description

@CesarRodriguezPereira

While attempting to subclass the flopy.modflow.mf.Modflow class, I've found that at least in that module, there are some "hard references" to the class. Take a look at the .load() method:

@staticmethod
def load(f, version='mf2005', exe_name='mf2005.exe', verbose=False,
         model_ws='.', load_only=None, forgive=False, check=True):

#Some more code until we get to the object instancing

    ml = Modflow(modelname, version=version, exe_name=exe_name,
                         verbose=verbose, model_ws=model_ws, **attribs)`

This makes it so that if you subclass the Modflow class in order to expand it's capabilities (maybe in order to better suit your needs or to adapt it to your use case), if you use the load method, you will return the parent class, as it is hard coded. This way, the methods you implement won't be applied to your object.

A simple change from staticmethod to class method, allows for subclassing to be way easier:

@classmethod
def load(cls,f, version='mf2005', exe_name='mf2005.exe', verbose=False,
         model_ws='.', load_only=None, forgive=False, check=True):

#Again, some more code until object instancing 

    ml = cls(modelname, version=version, exe_name=exe_name,
                         verbose=verbose, model_ws=model_ws, **attribs)

I've only found this with the flopy.modflow.mf.Modflow.load() method, but I have not looked elsewhere. This is a simple change that would make flopy's code to follow best practices, and allow better user experience. As of now, this is avoidable by overriding said method, but that just adds clutter to user code.

Just as a quick reference, here's Raymond Hettinger, Python core developer talking about this exact issue and its simple solution: Python's Class Development Toolkit

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions