By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,724 Members | 863 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,724 IT Pros & Developers. It's quick & easy.

how about a builtin "abstractmethod"?

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

st***@ferg.org (Stephen Ferg) wrote:
# 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

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.