Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class Controller {
- val path: String
- }
- class MyController extends Controller {
- override val path = "/home"
- }
- class Base(object):
- @property
- def path(self):
- raise NotImplementedError
- class SubClass(Base):
- path = 'blah'
- from abc import ABCMeta, abstractmethod, abstractproperty
- class A:
- __metaclass__ = ABCMeta
- def __init__(self):
- # ...
- pass
- @abstractproperty
- def a(self):
- pass
- @abstractmethod
- def b(self):
- pass
- class B(A):
- a = 1
- def b(self):
- pass
- from abc import ABCMeta, abstractmethod
- class A(metaclass=ABCMeta):
- def __init__(self):
- # ...
- pass
- @property
- @abstractmethod
- def a(self):
- pass
- @abstractmethod
- def b(self):
- pass
- class B(A):
- a = 1
- def b(self):
- pass
- class Controller(object):
- def __new__(cls, *args, **kargs):
- if not hasattr(cls,'path'):
- raise NotImplementedError("'Controller' subclasses should have a 'path' attribute")
- return object.__new__(cls,*args,**kargs)
- class C1(Controller):
- path = 42
- class C2(Controller):
- pass
- c1 = C1()
- # ok
- c2 = C2()
- # NotImplementedError: 'Controller' subclasses should have a 'path' attribute
- class _Controller(object):
- path = '' # There are better ways to declare attributes - see other answers
- class MyController(_Controller):
- path = '/Home'
- import abc
- class Controller(abc.ABC):
- path = NotImplemented # type: str
- class MyController(Controller):
- path = '/home'
- In [20]: class X:
- ...: def __init_subclass__(cls):
- ...: if not hasattr(cls, 'required'):
- ...: raise NotImplementedError
- In [21]: class Y(X):
- ...: required =5
- ...:
- In [22]: Y()
- Out[22]: <__main__.Y at 0x7f08408c9a20>
Add Comment
Please, Sign In to add comment