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, 2019 in Python by Anirudh
• 2,090 points
2,350 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, 2019 by Nymeria
• 3,560 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, 2019 in Python by Nisa
• 1,090 points
1,723 views
0 votes
1 answer

What is the Python 3 equivalent of “python -m SimpleHTTPServer”

The SimpleHTTPServer module has been merged into http.server in Python 3.0. ...READ MORE

answered Nov 28, 2020 in Python by Gitika
• 65,770 points
693 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,440 points
4,943 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, 2019 in Python by rajesh kumar
1,090 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,470 views
0 votes
1 answer
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, 2019 in Python by Nymeria
• 3,560 points
970 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP