Skip to content

Instantly share code, notes, and snippets.

@datakop
Created September 6, 2013 08:20
Show Gist options
  • Save datakop/6460975 to your computer and use it in GitHub Desktop.
Save datakop/6460975 to your computer and use it in GitHub Desktop.
Abstract classes in Pyhton. Decorator implementation for pure virtual functions.
# decorator
def pure_virtual(func):
def closure(*dt, **mp):
raise NotImplementedError("Method %s is pure virtual" % func.__name__)
return closure
class Abstarct(object):
""" Abstract class Class """
@pure_virtual
def virtual_method(self): pass # Empty Virtual method
# >>> Abstarct().virtual_method()
# Traceback (most recent call last):
# File "abstr.py", line .., in <module>
# Abstarct().virtual_method()
# File "abstr.py", line .., in closure
# raise NotImplementedError("Method %s is pure virtual" % func.__name__)
# NotImplementedError: Method virtual_method is pure virtual
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment