What is the problem?
The problem has been reported several times already, so I'm late to the party (here fore example #979).
To recap, assert keyword is not to be used as proposed in the example of the episode of Defensive Programming. The reason has been pointed out several times with various degrees of detail, but it boils down to that is basically a debug and testing phase tool, that can be bypassed at runtime by the interpreter. Python documentation states that basically evaluates to:
if __debug__:
if not expression: raise AssertionError
The episode of Defensive Programming focuses mainly on input validation. Input validation needs to stop the program every time an incorrect input is passed. if raise blocks are the standard and canonical way of doing this. For an example see this function in the scipy repository:
https://github.com/scipy/scipy/blob/v1.14.0/scipy/optimize/_minimize.py#L53-L775
I do not propose to also add the idea of exceptions neither try except blocks, since a novice user will not need this, I simple propose to replace assert blocks with if raise blocks. I will provide a pull request of this in the coming days.
Location of problem (optional)
https://github.com/swcarpentry/python-novice-inflammation/blob/main/episodes/10-defensive.md
What is the problem?
The problem has been reported several times already, so I'm late to the party (here fore example #979).
To recap,
assertkeyword is not to be used as proposed in the example of the episode of Defensive Programming. The reason has been pointed out several times with various degrees of detail, but it boils down to that is basically a debug and testing phase tool, that can be bypassed at runtime by the interpreter. Python documentation states that basically evaluates to:The episode of Defensive Programming focuses mainly on input validation. Input validation needs to stop the program every time an incorrect input is passed.
if raiseblocks are the standard and canonical way of doing this. For an example see this function in the scipy repository:https://github.com/scipy/scipy/blob/v1.14.0/scipy/optimize/_minimize.py#L53-L775
I do not propose to also add the idea of
exceptionsneithertry exceptblocks, since a novice user will not need this, I simple propose to replaceassertblocks withif raiseblocks. I will provide a pull request of this in the coming days.Location of problem (optional)
https://github.com/swcarpentry/python-novice-inflammation/blob/main/episodes/10-defensive.md