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,060 points
29 views

1 answer to this question.

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

0 votes
1 answer

What is the purpose of using lambda functions in Python?

The main purpose of anonymous functions come ...READ MORE

answered Jun 11 in Python by Nisa
• 1,090 points
26 views
+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
1,144 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
28 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
59 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 122 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
38 views