Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

77%
+5 −0
Q&A Use cases for raising a 'NotImplementedError' in Python

NotImplementedError should generally be viewed as indicating some design problem. You should not be reaching for it as a matter of course. Here are some potential times you might feel a desire to ...

posted 4mo ago by Derek Elkins‭

Answer
#1: Initial revision by user avatar Derek Elkins‭ · 2024-07-05T20:31:05Z (4 months ago)
`NotImplementedError` should generally be viewed as indicating some design problem. You should not be reaching for it as a matter of course.

Here are some potential times you might feel a desire to reach for `NotImplementedError`.

1. During development, you want to test some code that is partially implemented. Using `NotImplementedError` for the parts not yet implemented would be fine. Of course, these uses of `NotImplementedError` should be removed before you "release" the project.

2. A third-party library requires you to implement all methods of a class to use its functionality, but some methods don't make sense for your use-case. If the documentation states that some methods don't need to be implemented, then you'd be fine (but they'd probably already have default implementations that raised `NotImplementedError`). If not, then you don't know if it is safe to not implement one of the methods. Using `NotImplementedError` in this case should be viewed as a hack that may well cause unknown issues now and/or in the future. Arguably, being in this situation either way indicates poor design in the third party library.

3. You have a plugin system and a plugin may optionally provide certain functionality. `NotImplementedError` may seem like a good way to handle optional functionality that's not provided, but a better way is to query the plugin for different facets (or bundles) of coherent functionality. For example, if you had an IDE plugin that could support syntax highlighting or jump to definition among other things, you could have `getSyntaxHighlighterFacet()` and `getJumpToDefinitionFacet()` methods that the plugin must implement. These methods would return a coherent object if it supports that functionality, otherwise it would return `None`. This would allow the plugin loader to query what functionality is available and substitute a fallback for functionality that's not available. It would also avoid finding out some functionality is unavailable at the last possible moment where it might lead to unexpected behavior or require fallback logic to be written everywhere.