What is the equivalent of NotImplementedError using Python?

0 votes

Hi all. So basically I use Python 2.x 

And here, whenever we want to use a method which is abstract we define it as follows:

class Base:
    def foo(self):
        raise NotImplementedError("Subclasses should implement this!")

And later, let us say I forgot to override it. This is where I get the exception, right? 

IS there any nice way to consider a field to be marked as abstract? Or can it be done easily by changing and adding in the documentation string and considering that solves the issue?

All help appreciated, cheers!

Jan 17 in Python by Anirudh
• 2,050 points
15 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Hi, good question!

One simple answer to your question is YES! You can make use of the @property decorator just for this purpose.

Consider this instance - Let us say you have a field called "example", then is almost not possible to do this:

class Base(object):

    @property
    def example(self):
        raise NotImplementedError("Implement using Subclasses!")

And basically running the below code gives you a NotImplementedError exactly as expected!

c = Base()
print c.example
answered Jan 17 by Nymeria
• 3,500 points

Related Questions In Python

+2 votes
3 answers

what is the practical use of polymorphism in Python?

Polymorphism is the ability to present the ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,420 points
955 views
0 votes
2 answers

What is the use of Python language?

python is general purpose programming language.it very ...READ MORE

answered Mar 15 in Python by rajesh kumar
19 views
0 votes
1 answer

What is the use of raw_input function in Python?

raw_input fuction is no longer available in ...READ MORE

answered May 2, 2018 in Python by aayushi
• 750 points
38 views
0 votes
1 answer

What is the Python equivalent for a case/switch statement?

if x == 'a':  # Do the ...READ MORE

answered Jul 26, 2018 in Python by Priyaj
• 56,120 points
46 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
20 views
0 votes
1 answer
0 votes
1 answer

What is the recommended way to randomize a list of strings using Python?

Hi. Nice question. Here is the simplified answer ...READ MORE

answered Jan 18 in Python by Nymeria
• 3,500 points
30 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.