Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python 文化中的接口和协议

赖信涛
October 21, 2018
330

Python 文化中的接口和协议

2018 PyCon 中国演讲 PPT

赖信涛

October 21, 2018
Tweet

Transcript

  1. 3\WKRQ෈۸Ӿጱളݗ޾ܐᦓ
    Ӿࢵ Ӥၹ
    By laixintao

    View Slide


  2. Ø Luatable
    Ø C
    Ø Java ……
    Ø JavaScript!
    Ø Python
    __method__

    View Slide


  3. • “”
    • “xxx”
    • Dunder method

    View Slide

  4. http://lucumr.pocoo.org/2011/7/9/python-and-pola/

    View Slide

  5. View Slide




  6. ——Leonardo Rochael

    View Slide

  7. View Slide

  8. View Slide

  9. Python“”
    • Python “”


    View Slide



  10. !
    • '2/&
    • )%+*
    • %,"
    • 1+%)

    • -$%.
    • (#
    • 0

    • .%


    View Slide



  11. !
    • '2/&
    • )%+*
    • %,"
    • 1+%)

    • -$%.
    • (#
    • 0

    • .%


    View Slide

  12. __init__
    __new__
    __del__
    https://www.python.org/download/releases/2.2/descrintro/#__new__

    View Slide

  13. __new__

    View Slide

  14. __new__

    View Slide



  15. !
    • '2/&
    • )%+*
    • %,"
    • 1+%)

    • -$%.
    • (#
    • 0

    • .%


    View Slide


  16. https://www.python.org/dev/peps/pep-0207/
    https://docs.python.org/3/reference/expressions.html#value-comparisons

    obj1 __lt__x
    obj1 < x
    obj1.__lt__(x)
    x > obj1

    1. obj1 > x
    2. : obj1 < x
    3. :
    obj1.__lt__(x)

    View Slide

  17. @functools.total_ordering



    __eq__

    View Slide



  18. https://github.com/zestyping/q/blob/master/q.py

    View Slide


  19. • __pot__(self) +a
    • __add__(self, other) a + b

    __add__(self, other) b + a
    • __iadd__(self, other)
    • __int__(self)
    https://docs.python.org/3/library/operator.html

    View Slide

  20. View Slide



  21. !
    • '2/&
    • )%+*
    • %,"
    • 1+%)

    • -$%.
    • (#
    • 0

    • .%


    View Slide


  22. • __str__ str()

    • __repr__ repr()

    __str__
    • __bytes__ bytes()

    • __format__ format()

    • __dir__ dir()

    • __hash__ hash()

    https://dbader.org/blog/python-repr-vs-str

    View Slide

  23. format

    View Slide

  24. dir()
    https://github.com/laike9m/pdir2

    View Slide

  25. hash ()


    __eq__

    __eq__ hashable
    https://www.kawabangga.com/posts/1821
    https://docs.python.org/3/reference/datamodel.html#object.__hash__

    View Slide



  26. !
    • '2/&
    • )%+*
    • %,"
    • 1+%)

    • -$%.
    • (#
    • 0

    • .%


    View Slide


  27. __getattr__
    __setattr__
    __delattr__
    __getattribute__
    featurehttps://www.kawabangga.com/posts/2809


    View Slide


  28. View Slide


  29. https://docs.python.org/3/reference/datamodel.html#more-attribute-access-for-new-style-classes

    View Slide

  30. __setattr__

    View Slide



  31. !
    • '2/&
    • )%+*
    • %,"
    • 1+%)

    • -$%.
    • (#
    • 0

    • .%


    View Slide



  32. • __len__() len() __bool__() return self.__len__()
    • __getitem__() obj[key]
    • __setitem__() obj[key] = value
    • __missing__() obj[non_exist]
    • __delitem__() del obj[key]
    • __iter__()
    • __reversed__() reversed()

    __getattr__
    __setattr__
    __delattr__
    __getattribute__
    []
    .

    View Slide

  33. Python
    Python forhttp://effbot.org/zone/python-for-statement.htm

    __getitem__()
    for

    View Slide



  34. !
    • '2/&
    • )%+*
    • %,"
    • 1+%)

    • -$%.
    • (#
    • 0

    • .%


    View Slide


  35. • __instancecheck__(self, instance) isinstance(instance, class)
    • __subclasscheck__(self, subclass) issubclass(subclass, class)

    View Slide

  36. sized
    https://github.com/python/cpython/blob/9d4712bc8f26bf1d7e62
    6b53ab092fe030bcd68d/Lib/_collections_abc.py#L359

    View Slide



  37. !
    • '2/&
    • )%+*
    • %,"
    • 1+%)

    • -$%.
    • (#
    • 0

    • .%


    View Slide

  38. First Class Object





    https://dbader.org/blog/python-first-class-functions
    https://www.kawabangga.com/posts/2613

    View Slide



  39. __call__()

    View Slide



  40. !
    • '2/&
    • )%+*
    • %,"
    • 1+%)

    • -$%.
    • (#
    • 0

    • .%


    View Slide




  41. • __enter__()
    • __exit__()
    https://www.kawabangga.com/posts/2010

    View Slide


  42. print

    View Slide

  43. contextlib.contextmanager

    View Slide



  44. !
    • '2/&
    • )%+*
    • %,"
    • 1+%)

    • -$%.
    • (#
    • 0

    • .%


    View Slide

  45. @property
    https://www.kawabangga.com/posts/2302

    View Slide


  46. View Slide

  47. ……
    •descr.__get__(self, obj, type=None) -> value
    •descr.__set__(self, obj, value) -> None
    •descr.__delete__(self, obj) -> None
    https://docs.python.org/3.7/howto/descriptor.html

    View Slide


  48. ORM

    View Slide

  49. function
    method
    • method __dict__ function
    • function __get__()
    method
    https://docs.python.org/3.7/howto/descriptor.html#functions-and-methods

    View Slide



  50. !
    • '2/&
    • )%+*
    • %,"
    • 1+%)

    • -$%.
    • (#
    • 0

    • .%


    View Slide


  51. • __copy__(self) copy.copy()
    • __deepcopy__(self, memodict={}) copy.deepcopy()

    View Slide

  52. pickle
    • object.__getnewargs_ex__()
    • object.__getnewargs__()
    • object.__getstate__()
    • object.__setstate__(state)
    • object.__reduce__()
    • object.__reduce_ex__(protocol)
    https://www.kawabangga.com/posts/2782

    View Slide

  53. @laixintao
    www.kawabangga.com
    SRE
    Python

    View Slide