2018 PyCon 中国演讲 PPT
3\WKRQ۸Ӿጱളݗܐᦓ Ӿࢵ ӤၹBy laixintao
View Slide
Ø LuatableØ C Ø Java ……Ø JavaScript!Ø Python__method__
• • “”• “xxx”• Dunder method
http://lucumr.pocoo.org/2011/7/9/python-and-pola/
——Leonardo Rochael
Python“”• Python “”•
• !• '2/&• )%+*• %,"• 1+%)• • -$%.• (#• 0• • .%
__init____new____del__https://www.python.org/download/releases/2.2/descrintro/#__new__
__new__
https://www.python.org/dev/peps/pep-0207/ https://docs.python.org/3/reference/expressions.html#value-comparisonsobj1 __lt__x obj1 < x obj1.__lt__(x)x > obj1 1. obj1 > x2. : obj1 < x3. :obj1.__lt__(x)
@functools.total_ordering• • __eq__
https://github.com/zestyping/q/blob/master/q.py
• __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
• __str__ str() • __repr__ repr() __str__• __bytes__ bytes() • __format__ format() • __dir__ dir() • __hash__ hash() https://dbader.org/blog/python-repr-vs-str
format
dir()https://github.com/laike9m/pdir2
hash ()• • __eq__• __eq__ hashable https://www.kawabangga.com/posts/1821https://docs.python.org/3/reference/datamodel.html#object.__hash__
__getattr____setattr____delattr____getattribute__ featurehttps://www.kawabangga.com/posts/2809
https://docs.python.org/3/reference/datamodel.html#more-attribute-access-for-new-style-classes
__setattr__
• __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__[] .
Python Python forhttp://effbot.org/zone/python-for-statement.htm __getitem__() for
• __instancecheck__(self, instance) isinstance(instance, class)• __subclasscheck__(self, subclass) issubclass(subclass, class)
sized https://github.com/python/cpython/blob/9d4712bc8f26bf1d7e626b53ab092fe030bcd68d/Lib/_collections_abc.py#L359
First Class Object• • • • https://dbader.org/blog/python-first-class-functionshttps://www.kawabangga.com/posts/2613
__call__()
• • __enter__()• __exit__()https://www.kawabangga.com/posts/2010
print
contextlib.contextmanager
@property https://www.kawabangga.com/posts/2302
……•descr.__get__(self, obj, type=None) -> value•descr.__set__(self, obj, value) -> None•descr.__delete__(self, obj) -> Nonehttps://docs.python.org/3.7/howto/descriptor.html
ORM
function method • method __dict__ function• function __get__() methodhttps://docs.python.org/3.7/howto/descriptor.html#functions-and-methods
• __copy__(self) copy.copy() • __deepcopy__(self, memodict={}) copy.deepcopy()
pickle• object.__getnewargs_ex__()• object.__getnewargs__()• object.__getstate__()• object.__setstate__(state)• object.__reduce__()• object.__reduce_ex__(protocol)https://www.kawabangga.com/posts/2782
@laixintaowww.kawabangga.comSRE Python