Guest User

Untitled

a guest
Nov 22nd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. abstract class Controller {
  2.  
  3. val path: String
  4.  
  5. }
  6.  
  7. class MyController extends Controller {
  8.  
  9. override val path = "/home"
  10.  
  11. }
  12.  
  13. class Base(object):
  14. @property
  15. def path(self):
  16. raise NotImplementedError
  17.  
  18.  
  19. class SubClass(Base):
  20. path = 'blah'
  21.  
  22. from abc import ABCMeta, abstractmethod, abstractproperty
  23.  
  24.  
  25. class A:
  26. __metaclass__ = ABCMeta
  27.  
  28. def __init__(self):
  29. # ...
  30. pass
  31.  
  32. @abstractproperty
  33. def a(self):
  34. pass
  35.  
  36. @abstractmethod
  37. def b(self):
  38. pass
  39.  
  40.  
  41. class B(A):
  42. a = 1
  43.  
  44. def b(self):
  45. pass
  46.  
  47. from abc import ABCMeta, abstractmethod
  48.  
  49.  
  50. class A(metaclass=ABCMeta):
  51. def __init__(self):
  52. # ...
  53. pass
  54.  
  55. @property
  56. @abstractmethod
  57. def a(self):
  58. pass
  59.  
  60. @abstractmethod
  61. def b(self):
  62. pass
  63.  
  64.  
  65. class B(A):
  66. a = 1
  67.  
  68. def b(self):
  69. pass
  70.  
  71. class Controller(object):
  72. def __new__(cls, *args, **kargs):
  73. if not hasattr(cls,'path'):
  74. raise NotImplementedError("'Controller' subclasses should have a 'path' attribute")
  75. return object.__new__(cls,*args,**kargs)
  76.  
  77. class C1(Controller):
  78. path = 42
  79.  
  80. class C2(Controller):
  81. pass
  82.  
  83.  
  84. c1 = C1()
  85. # ok
  86.  
  87. c2 = C2()
  88. # NotImplementedError: 'Controller' subclasses should have a 'path' attribute
  89.  
  90. class _Controller(object):
  91. path = '' # There are better ways to declare attributes - see other answers
  92.  
  93. class MyController(_Controller):
  94. path = '/Home'
  95.  
  96. import abc
  97.  
  98.  
  99. class Controller(abc.ABC):
  100. path = NotImplemented # type: str
  101.  
  102.  
  103. class MyController(Controller):
  104. path = '/home'
  105.  
  106. In [20]: class X:
  107. ...: def __init_subclass__(cls):
  108. ...: if not hasattr(cls, 'required'):
  109. ...: raise NotImplementedError
  110.  
  111. In [21]: class Y(X):
  112. ...: required =5
  113. ...:
  114.  
  115. In [22]: Y()
  116. Out[22]: <__main__.Y at 0x7f08408c9a20>
Add Comment
Please, Sign In to add comment