how about a builtin "abstractmethod"?

S

Stephen Ferg

Python has a builtin class for staticmethod. Seems to me that Python
should also have a builtin class for abstractmethod. Something like
this...

#######################################

# simulated implementation of a new builtin class: abstractmethod
def abstractmethod(f):
methodName = f.__name__
def temp(self, *args, **kwargs):
raise NotImplementedError(
"Attempt to invoke unimplemented abstract method %s"
% methodName)
return temp

# example of using proposed builtin class: abstractmethod
class TestClass:
@abstractmethod
def TestMethod(self): pass


t = TestClass()
t.TestMethod() # call to abstract method raises exception
 
J

Josiah Carlson

# simulated implementation of a new builtin class: abstractmethod
def abstractmethod(f):
methodName = f.__name__
def temp(self, *args, **kwargs):
raise NotImplementedError(
"Attempt to invoke unimplemented abstract method %s"
% methodName)
return temp

# example of using proposed builtin class: abstractmethod
class TestClass:
@abstractmethod
def TestMethod(self): pass
^^^^^
I don't believe that using a single line for such definitions are
considered Pythonic.


I prefer...

class TestClass:
def TestMethod(self):
raise NotImplementedError

Sure, you don't get the function name in your textual exception raise,
but you do get the function call in your traceback, and save yourself
having to use decorators when they are certainly not necessary, and
don't improve the readability nor understandability of the code.

- Josiah
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,687
Latest member
IngridXxj

Latest Threads

Top