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.

Comments on Use cases for raising a 'NotImplementedError' in Python

Parent

Use cases for raising a 'NotImplementedError' in Python

+6
−0

What are some legitimate use cases for raising a NotImplementedError exception in Python? How can it help express a code author's intentions to aid code maintenance and/or further development of code?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+5
−0

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

what about abstract methods? (2 comments)
what about abstract methods?
mr Tsjolder‭ wrote 4 months ago

This answer is missing one of the main reasons in the context of OOP: indicating that a method (and therefore its class) is abstract.

Derek Elkins‭ wrote 4 months ago

You should use the @abstractmethod decorator for this which will prevent a class that doesn't implement all the methods from being instantiated at all rather than finding out a method wasn't implemented right as you go to use it. Suffice it to say that throwing "NotImplemented" exceptions is not the mechanism that the vast majority of OO languages use to indicate that a method is abstract. In most cases, that wouldn't even make sense.