They behave differently in Python2.x and Python3.x .
Python3.6
# importing abstract base classes module
import abc
class GetterSetter(abc.ABC):
'''
ABSTRACT BASE CLASSES:
- An abstract base class is a kind of 'model' for other classes to be defined.
- It is not designed to construct instances, but can be subclassed by regular classes
- Abstract classes can define interface, or methods that must be implemented by its subclasses.
'''
# Abstract classes are not designed to be instantiated, only to be subclassed
# decorator for abstract class
@abc.abstractmethod
def set_val(self, input):
"""set the value in the instance"""
return
@abc.abstractmethod
def get_val(self):
"""Get and return a value from the instance..."""
return
# Inheriting from the above abstract class
class MyClass(GetterSetter):
# methods overriding in the GetterSetter
def set_val(self, input):
self.val = input
def get_val(self):
return self.val
# Instantiate
x = MyClass()
print(x) # prints the instance <__main__.MyClass object at 0x10218ee48>
x = GetterSetter() #throws error, abstract classes can't be instantiated
Python2.x
import abc
class GetterSetter(object):
# meta class is used to define other classes
__metaclass__ = abc.ABCMeta
# decorator for abstract class
@abc.abstractmethod
def set_val(self, input):
"""set the value in the instance"""
return
@abc.abstractmethod
def get_val(self):
"""Get and return a value from the instance..."""
return
# Inheriting from the above abstract class
class MyClass(GetterSetter):
# methods overriding in the GetterSetter
def set_val(self, input):
self.val = input
def get_val(self):
return self.val
# Instantiate
x = GetterSetter()
print(x)
x = GetterSetter() #throws error, abstract classes can't be instantiated
Check my answer here.