Python 文化中的接口和协议

F750dea9cdc5f6cfb13cefc3273de198?s=47 赖信涛
October 21, 2018
180

Python 文化中的接口和协议

2018 PyCon 中国演讲 PPT

F750dea9cdc5f6cfb13cefc3273de198?s=128

赖信涛

October 21, 2018
Tweet

Transcript

  1. 2.

     Ø Luatable Ø C  Ø Java …… Ø

    JavaScript! Ø Python   __method__
  2. 5.
  3. 7.
  4. 8.
  5. 10.

     •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  6. 11.

     •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  7. 15.

     •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  8. 16.
  9. 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
  10. 20.
  11. 21.

     •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  12. 22.

     • __str__ str()   • __repr__ repr() 

     __str__ • __bytes__ bytes() • __format__ format() • __dir__ dir() • __hash__ hash() https://dbader.org/blog/python-repr-vs-str
  13. 25.

    hash () •    •   __eq__

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

     •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  15. 28.
  16. 31.

     •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  17. 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__ []  . 
  18. 34.

     •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  19. 37.

     •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  20. 38.

    First Class Object •    •  

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

     •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  22. 41.

      •    •   __enter__()

    •   __exit__() https://www.kawabangga.com/posts/2010
  23. 44.

     •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  24. 46.
  25. 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
  26. 49.

    function method   •  method  __dict__ 

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

     •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  28. 52.

    pickle • object.__getnewargs_ex__() • object.__getnewargs__() • object.__getstate__() • object.__setstate__(state) •

    object.__reduce__() • object.__reduce_ex__(protocol) https://www.kawabangga.com/posts/2782