Интерфейсы в Python

3,854 views

Published on

Обзор различных способов описания интерфейсов в Питоне

Published in: Technology
4 Comments
2 Likes
Statistics
Notes
No Downloads
Views
Total views
3,854
On SlideShare
0
From Embeds
0
Number of Embeds
1,193
Actions
Shares
0
Downloads
21
Comments
4
Likes
2
Embeds 0
No embeds

No notes for slide

Интерфейсы в Python

  1. 1. Интерфейсы Andrew Svetlovandrew.svetlov@gmail.com asvetlov.blogspot.com
  2. 2. Интерфейсы● Duck Typing● ABC● Zope Interface
  3. 3. Duck Typingclass A: >>> a = A() def f(self): >>> b = B(); print(A.f) >>> a.f()class B: A.f def f(self): >>> b.f() print(B.f) b.f
  4. 4. Pro/Contra● Естественная ● Неявность запись интерфейса● Ничего лишнего ● Сложность само-● hasattr vs isinstance документирования ● a.x => Point ???
  5. 5. Iterable● __iter__ class A: def __getitem__(self, i):● __getitem__ if i < 3: пока не IndexError return i raise IndexError("out of range") >>> a = A() >>> i = iter(a) >>> list(i) [0, 1, 2]
  6. 6. Конструктор dict● Аргумент может быть:● dict● Sequence of 2-elem sequences● Mapping??? .keys!!!● Iterable
  7. 7. Наследованиеclass Base: def f(self): raise NotImplementedError()class A(Base): def f(self): return 1>>> a = A()>>> isinstance(a, Base)True>>> b = Base() # ???
  8. 8. ABC (Abstract Base Classes)class Base(abc.Meta): @abc.abstractmethod def f(self): return 0class A(Base): def f(self): return super().f() + 1>>> a = A()>>> isinstance(a, Base)True>>> b = Base() # ???Cant instantiate abstract class Base with abstract methods f>>> a.f()1
  9. 9. collections.abc● Hashable ● Mapping● Iterable ● MutableMapping● Iterator ● MappingView● Sized ● KeysView● Container ● ItemsView● Callable● Set ● ValuesView● MutableSet ● Sequence● ByteString ● MutableSequence
  10. 10. Самодельное множествоfrom collections import Set >>> s1 = S({1, 2})class S(Set): >>> 2 in s1 def __init__(self, s): True super().__init__() >>> s2 = S({2, 3}) self.s = frozenset(s) >>> s3 = s1|s2 def __contains__(self, i): >>> list(s3) return i in self.s [1, 2, 3] def __iter__(self): return iter(self.s) >>> s4 = s1 - s2 def __len__(self): >>> list(s4) return len(self.s) [1]
  11. 11. Необязательные методы● Наличие метода● Проверка на None● Исключение NotImplementedError● Значение NotImplemented
  12. 12. Наличие метода и Noneclass A: class B(A): post = None def pre(self): def do(self): print(B.pre) if hasattr(self, def post(self): pre): print(B.post) self.pre() print(A.do) if self.post is not None: >>> b = B() self.post() >>> b.do()>>> a = A() B.pre>>> a.do() A.doA.do B.post
  13. 13. NotImplementedErrorclass A: class B(A): def pre(self): def pre(self): raise NotImplementedError( print(B.pre) implement pre method) def do(self): >>> a = A() try: >>> a.do() self.pre() A.do except NotImplementedError: >>> b = B() pass print(A.do) >>> b.do() B.pre a.do
  14. 14. NotImplementedclass A: class B: def override(self): def override(self): return NotImplemented return overriden def do(self): >>> a = A() val = self.override() >>> a.do() if (val is not NotImplemented): default return val >>> b = B() else: >>> b.do() return default overriden
  15. 15. Zope Interfaceclass IFile(Interface): body = Attribute(Contents of the file.)class ISize(Interface): def getSize(): Return the size of an object.class File(object): implements(IFile) body = foo barclass FileSize(object): implements(ISize) __used_for__ = IFile def __init__(self, context): self.context = context def getSize(self): return len(self.context.body)
  16. 16. Адаптерыregistry = AdapterRegistry()def hook(provided, obj): adapter = registry.lookup1(providedBy(obj), provided, ) return adapter(object)adapter_hooks.append(hook)>>> file = File()>>> size = ISize(file)>>> size.getSize()
  17. 17. Вопросы? Andrew Svetlovandrew.svetlov@gmail.com asvetlov.blogspot.com

×