Hi @knutfrode, @gauteh ,
I'm a proposer of yet-to-be integrated feature that changes reader_shape.py (PR #1460).
This implementation stayed away from public methods but it changed the signature of reader_shape.Reader.__on_land__().
Tests were added to test_reader_shape.py and all tests passed after the latests changes from the main opendrift branch.
However, the reader crashes when the basemodel.__init__() tries to access __on_land__() directly.
This change was introduced by @poplarShift on a691efa (line 934) replacing the use of reader_shape.Reader.is_inside().
My approach was to change a private method's signature instead of creating another method behind it, to avoid clutter. I felt confident to only write unit tests for local client code since I assumed __on_land__() to be a non-public method do to the underscores. I now see this was wrong to assume this since dunder notation does not signal non-public methods necessarily.
I can change back the signature and implement my changes differently but I see a set of issues here:
- If
__on_land__ is to be a non-public method (there are no private methods in python) then it shouldn't be accessed from outside its class. If there is need for that functionality, that needs to be in a public method (no underscores).
- The use of double underscores "dunder" methods is reserved for python magic functions. We shouldn't implement those. If indeed the method needs to be non-public, choose between a weak non-public that still can be used outside (though highly discouraged) using a single leading underscore (e.g.
_weak_non_public_method()) or a strong non-public with a leading double underscore (e.g. __strong_non_public_method()) that will get name-mangled making it impossible to use it directly outside of the class.
Opendrift's codebase is extensive and has already a lot of complex links. Please adopt the intended use of underscores in python to signal methods that will not be called from outside the class so that developers are confident to change classes internally without interfering with their interfaces.
Cheers.
João.
Hi @knutfrode, @gauteh ,
I'm a proposer of yet-to-be integrated feature that changes
reader_shape.py(PR #1460).This implementation stayed away from public methods but it changed the signature of
reader_shape.Reader.__on_land__().Tests were added to
test_reader_shape.pyand all tests passed after the latests changes from the main opendrift branch.However, the reader crashes when the
basemodel.__init__()tries to access__on_land__()directly.This change was introduced by @poplarShift on a691efa (line 934) replacing the use of
reader_shape.Reader.is_inside().My approach was to change a private method's signature instead of creating another method behind it, to avoid clutter. I felt confident to only write unit tests for local client code since I assumed
__on_land__()to be a non-public method do to the underscores. I now see this was wrong to assume this since dunder notation does not signal non-public methods necessarily.I can change back the signature and implement my changes differently but I see a set of issues here:
__on_land__is to be a non-public method (there are no private methods in python) then it shouldn't be accessed from outside its class. If there is need for that functionality, that needs to be in a public method (no underscores)._weak_non_public_method()) or a strong non-public with a leading double underscore (e.g.__strong_non_public_method()) that will get name-mangled making it impossible to use it directly outside of the class.Opendrift's codebase is extensive and has already a lot of complex links. Please adopt the intended use of underscores in python to signal methods that will not be called from outside the class so that developers are confident to change classes internally without interfering with their interfaces.
Cheers.
João.