import abc
class SetterGetter(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def set_val(self, inp):
return
@abc.abstractmethod
def get_val(self):
return
class TryAbsMethod(SetterGetter):
def get_val(self):
return self.inp
def set_valz(self, inp):
self.inp = inp
a = TryAbsMethod()
print(a)
As per tutorial, I was expecting excption from above code but instead I got output from python which is strange, Can someone please explain me why?
Output got
/usr/bin/python3.4 /home/murtuza/MyCodes/abstract.py
<__main__.TryAbsMethod object at 0x7f93ba8ab048>
Process finished with exit code 0
Exception Which is expected
TypeError: Can't instantiate abstract class TryAbsMethod with abstract method set_val
asked Sep 7, 2015 at 4:49
Murtuza Z
6,0951 gold badge32 silver badges56 bronze badges
-
Which tutorial is it that you're using?user707650– user7076502015年09月07日 04:53:59 +00:00Commented Sep 7, 2015 at 4:53
-
@Evert: shop.oreilly.com/product/0636920040057.do#tab_02_2Murtuza Z– Murtuza Z2015年09月07日 05:09:39 +00:00Commented Sep 7, 2015 at 5:09
1 Answer 1
You're using Python 3.x. In Python 3.x, metaclass declaring syntax is changed.
class SetterGetter(object, metaclass=abc.ABCMeta): # <---
@abc.abstractmethod
def set_val(self, inp):
return
@abc.abstractmethod
def get_val(self):
return
See Customizing class creation - Data model - Python 3.x documentation, PEP3115 - Metaclasses in Python 3
answered Sep 7, 2015 at 4:54
falsetru
371k69 gold badges770 silver badges660 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
falsetru
@RajeshChoudhary, Please post a question, instead of commenting on irrelevant answers.
Rajesh Choudhary
this is my question please see this stackoverflow.com/questions/32519620/…
lang-py