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

Python 文化中的接口和协议

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for 赖信涛 赖信涛
October 21, 2018
610

Python 文化中的接口和协议

2018 PyCon 中国演讲 PPT

Avatar for 赖信涛

赖信涛

October 21, 2018
Tweet

Transcript

  1.  Ø Luatable Ø C  Ø Java …… Ø

    JavaScript! Ø Python   __method__
  2.  •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  3.  •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  4.  •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  5.  •   __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
  6.  •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  7.  • __str__ str()   • __repr__ repr() 

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

    •  __eq__  hashable https://www.kawabangga.com/posts/1821 https://docs.python.org/3/reference/datamodel.html#object.__hash__
  9.  •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  10.  •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  11.    • __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__ []  . 
  12.  •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  13.  •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  14. First Class Object •    •  

    •   •     https://dbader.org/blog/python-first-class-functions https://www.kawabangga.com/posts/2613
  15.  •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  16.   •    •   __enter__()

    •   __exit__() https://www.kawabangga.com/posts/2010
  17.  •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  18. …… •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
  19. function method   •  method  __dict__ 

    function • function __get__()    method https://docs.python.org/3.7/howto/descriptor.html#functions-and-methods
  20.  •  ! • '2/& • )%+* • 

    %," • 1+%) •  • -$%. • (# • 0 •  • .% 
  21. pickle • object.__getnewargs_ex__() • object.__getnewargs__() • object.__getstate__() • object.__setstate__(state) •

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