471,602 Members | 1,296 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,602 software developers and data experts.

how about a builtin "abstractmethod"?

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
1 2052

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.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.