Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

• • “” • “xxx” • Dunder method

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

——Leonardo Rochael

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Python“” • Python “” •

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

__new__

Slide 14

Slide 14 text

__new__

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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)

Slide 17

Slide 17 text

@functools.total_ordering • • __eq__

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

• __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

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

format

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

__setattr__

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

• __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__ [] .

Slide 33

Slide 33 text

Python Python forhttp://effbot.org/zone/python-for-statement.htm __getitem__() for

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

__call__()

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

print

Slide 43

Slide 43 text

contextlib.contextmanager

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

Slide 47

Slide 47 text

…… •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

Slide 48

Slide 48 text

ORM

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

@laixintao www.kawabangga.com SRE Python