Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Python 文化中的接口和协议
Search
赖信涛
October 21, 2018
0
480
Python 文化中的接口和协议
2018 PyCon 中国演讲 PPT
赖信涛
October 21, 2018
Tweet
Share
Featured
See All Featured
Visualization
eitanlees
146
15k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.2k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
Speed Design
sergeychernyshev
25
690
Rails Girls Zürich Keynote
gr2m
94
13k
The Cost Of JavaScript in 2023
addyosmani
46
7k
Unsuck your backbone
ammeep
669
57k
Side Projects
sachag
452
42k
GraphQLとの向き合い方2022年版
quramy
44
13k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.3k
Code Review Best Practice
trishagee
65
17k
Transcript
3\WKRQ۸Ӿጱളݗܐᦓ Ӿࢵ Ӥၹ By laixintao
Ø Luatable Ø C Ø Java …… Ø
JavaScript! Ø Python __method__
• • “” • “xxx” • Dunder method
http://lucumr.pocoo.org/2011/7/9/python-and-pola/
None
——Leonardo Rochael
None
None
Python“” • Python “” •
• ! • '2/& • )%+* •
%," • 1+%) • • -$%. • (# • 0 • • .%
• ! • '2/& • )%+* •
%," • 1+%) • • -$%. • (# • 0 • • .%
__init__ __new__ __del__ https://www.python.org/download/releases/2.2/descrintro/#__new__
__new__
__new__
• ! • '2/& • )%+* •
%," • 1+%) • • -$%. • (# • 0 • • .%
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)
@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
None
• ! • '2/& • )%+* •
%," • 1+%) • • -$%. • (# • 0 • • .%
• __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/1821 https://docs.python.org/3/reference/datamodel.html#object.__hash__
• ! • '2/& • )%+* •
%," • 1+%) • • -$%. • (# • 0 • • .%
__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__
• ! • '2/& • )%+* •
%," • 1+%) • • -$%. • (# • 0 • • .%
• __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
• ! • '2/& • )%+* •
%," • 1+%) • • -$%. • (# • 0 • • .%
• __instancecheck__(self, instance) isinstance(instance, class) • __subclasscheck__(self, subclass) issubclass(subclass,
class)
sized https://github.com/python/cpython/blob/9d4712bc8f26bf1d7e62 6b53ab092fe030bcd68d/Lib/_collections_abc.py#L359
• ! • '2/& • )%+* •
%," • 1+%) • • -$%. • (# • 0 • • .%
First Class Object • •
• • https://dbader.org/blog/python-first-class-functions https://www.kawabangga.com/posts/2613
__call__()
• ! • '2/& • )%+* •
%," • 1+%) • • -$%. • (# • 0 • • .%
• • __enter__()
• __exit__() https://www.kawabangga.com/posts/2010
print
contextlib.contextmanager
• ! • '2/& • )%+* •
%," • 1+%) • • -$%. • (# • 0 • • .%
@property https://www.kawabangga.com/posts/2302
…… •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
ORM
function method • method __dict__
function • function __get__() method https://docs.python.org/3.7/howto/descriptor.html#functions-and-methods
• ! • '2/& • )%+* •
%," • 1+%) • • -$%. • (# • 0 • • .%
• __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
@laixintao www.kawabangga.com SRE Python