[Tutor] abc module question
Albert-Jan Roskam
fomcl at yahoo.com
Mon Sep 1 15:54:47 CEST 2014
Hi,
I was playing with abc for the first time (never ever had any use for it). It works nicely with methods and properties.
But is there also a way to enforce that class Concrete has an attribute 'foo' of type 'list'? I could of course write a 'foo' property, but I was hoping there was a direct way,
import sys
import abc
class Abstract(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def __init__(self):
pass
@abc.abstractmethod
def __getitem__(self, key):
raise NotImplementedError("__getitem__ method required!")
@abc.abstractproperty
def encoding(self):
raise NotImplementedError("encoding property required!")
@abc.abstractproperty
def foo(self):
assert isinstance(self.foo, list)
class Concrete(Abstract):
def __init__(self):
self.foo = [] # it could be solved by making foo a property
def __getitem__(self, key):
pass
@property
def encoding(self):
return "some encoding"
c = Concrete()
Traceback (most recent call last):
File "C:\TEMP\abc_foo.py", line 46, in <module>
c = Concrete()
TypeError: Can't instantiate abstract class Concrete with abstract methods foo
Regards,
Albert-Jan
PS: Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a
fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
More information about the Tutor
mailing list