167
PYTHON DATA MODEL 특특 Moon Yong Joon 1

python data model 이해하기

Embed Size (px)

Citation preview

PowerPoint

Python data ModelMoon Yong Joon1

1. 2. 3. ABC

2

1. Moon Yong Joon3

Moon Yong Joon4

Meta Class5

. type 6

Instance ofInstance of

type type , tuple, namespace dict 7 type(,,namespace)

: type class 8

: class 9

Type class 10

type type 11

type : type 12['Camembert'] {'x': 42, '__module__': '__main__', '__doc__': None, 'howdy': , '__dict__': , '__weakref__': } 42 Howdy, John

class Moon Yong Joon13

14

metaclass type class

15

metaclass type A

16

namespace 17

type namespaceOnce the appropriate metaclass has been identified, then the class namespace is prepared. 18__prepare__(,)

type namespace type __prepare__ namespace 19

Metaclass namespace metaclass namespace classmethod 20namespace = metaclass.__prepare__(name, bases, **kwds)

object21

body . exec(body, globals(), namespace) 22

object Once the class namespace has been populated by executing the class body,

the class object is created by calling metaclass(name, bases, namespace, **kwds) 23

meta classMoon Yong Joon24

25

: type Meta type.__new__ 26

27

Meta metaclass A 28

: A Meta A namespace A 29the appropriate metaclass is determined ==>

the class namespace is prepared ===> {'__init__': , '__doc__': None, 'four': , '__dict__': , ' abc': 'abc', 'two': , '__weakref__': , '__module__': '__main__', 'three': , 'one': }

Moon Yong Joon30

31

Type Class type 32

Object Class Object Class class instance 33

Class Class class instance 34

Meta Class Meta Class class instance 35

2.

Moon Yong Joon36

37

abc.ABCMeta 38

Instance ofInstance of

abc.ABCMeta metaclass type 39

40

abc.ABC class 41

abc.ABC 42

43

abc.ABCMeta metaclass MyABC MyCon MyABC 44

45

abc.ABCMeta metaclass MyABC register class 46

: 47

48

register 49

50

abc abstractmethod, abstractclassmethod, abstractstaticmethod, abstractproperty 51

decorator 52 @abstractmethod def (self, ) : @abstractclassmethod def (cls, ) : @abstractstaticmethod def ( ) :

abstractmethod 53

: abc abstractmethod instance method 54

: abc abstractmethod 55

/ 56

abc abstractclassmethod /abstractstaticmethod 57

58

59

property decorator 60 @abstractproperty def (self, ) :

abstractproperty abstractproperty property 61

Abstractmethod abstractmethod property 62

3. ABCMoon Yong Joon63

Number Moon Yong Joon64

Number 65

numbers numbers 66

['ABCMeta', 'Complex', 'Integral', 'Number', 'Rational', 'Real', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'abstractmethod']

numbers numbers 67

Number check68

Number issubclass Numbers 69

Number isinstance instance Numbers instance 70

collections.ABC Moon Yong Joon71

collection 72

collections.abc collections.abc ,, dict, set 73['AsyncIterable', 'AsyncIterator', 'Awaitable', 'ByteString', 'Callable', 'Container', 'Coroutine', 'Generator', 'Hashable', 'ItemsView', 'Iterable', 'Iterator', 'KeysView', 'Mapping', 'MappingView', 'MutableMapping', 'MutableSequence', 'MutableSet', 'Sequence', 'Set', 'Sized', 'ValuesView', ]

collections.abc diagram collections.abc class diagram74

Fluent python

collections.abc 75ABCInherits fromAbstract MethodsMixin MethodsContainer__contains__Hashable__hash__Iterable__iter__IteratorIterable__next__ __iter__GeneratorIteratorsend,throw close,__iter__,__next__Sized__len__Callable__call__SequenceSized,Iterable,Container__getitem__,__len__ __contains__,__iter__,__reversed__, index, andcountMutableSequenceSequence__getitem__,__setitem__,__delitem__,__len__,insert InheritedSequence methods and append,reverse,extend, pop,remove, and__iadd__ByteStringSequence__getitem__,__len__ InheritedSequence methods

collections.abc 76ABCInherits fromAbstract MethodsMixin MethodsSetSized,Iterable,Container__contains__,__iter__,__len__ __le__,__lt__,__eq__,__ne__, __gt__,__ge__,__and__, __or__,__sub__,__xor__, andisdisjointMutableSetSet__contains__,__iter__,__len__,add,discard InheritedSet methods andclear, pop,remove,__ior__,__iand__, __ixor__, and__isub__MappingSized,Iterable,Container__getitem__,__iter__,__len__ __contains__,keys,items,values, get,__eq__, and__ne__MutableMappingMapping__getitem__,__setitem__,__delitem__,__iter__,__len__ InheritedMappingmethods and pop,popitem,clear,update, andsetdefaultMappingViewSized __len__ItemsViewMappingView,Set __contains__,__iter__KeysViewMappingView,Set __contains__,__iter__ValuesViewMappingView __contains__,__iter__

collections.abc 77ABCInherits fromAbstract MethodsMixin MethodsAwaitable__await__CoroutineAwaitablesend,throw closeAsyncIterable__aiter__AsyncIteratorAsyncIterable__anext__ __aiter__

class78

classContainer/Hashable/Sized/Iterable/Callable 79ABCAbstract MethodsContainer__contains__Hashable__hash__Iterable__iter__Sized__len__Callable__call__

class Container/Hashable/Sized/Iterable/Callable 80

Iterator/Generator 81

Iterator/Generator Iterator Iterable Generator Iterator 82

Iterator sequence iter() itoractor

83

Iterator Iterator class 84

iterable iterator Container iterable iterator 85

SEQUENCE 86

SEQUENCE class87ABCInherits fromAbstract MethodsMixin MethodsContainer__contains__Iterable__iter__Sized__len__Callable__call__SequenceSized,Iterable,Container__getitem__,__len__ __contains__, __iter__, __reversed__, index, andcountMutableSequenceSequence__getitem__,__setitem__,__delitem__,__len__,insert InheritedSequence methods and append,reverse,extend, pop,remove, and__iadd__ByteStringSequence__getitem__,__len__ InheritedSequence methods

Sequence class diagram Sequence class diagram 88

Fluent python

Sequence Sized, Iterabel, Container {'__iter__', '__len__', '__contains__'} 89

(, , ) {'__iter__', '__len__', '__contains__'}

Sequence Sequence 90(, , ){'count', 'index', '__reversed__', '__getitem__', '__iter__', '__contains__'}

SET 91

Set Sized, Iterabel, Container {'__iter__', '__len__', '__contains__'} 92(, , ) {'__iter__', '__len__', '__contains__'}

Set Set 93(, , ) {'__hash__', '__rand__', '_from_iterable', '__lt__', 'isdisjoint', '__or__', '__and__', '__ge__', '_hash', '__rxor__', '__ror__', '__eq__', '__le__', '__xor__', '__rsub__', '__gt__', '__sub__'}

MAPPING 94

MAPPING 95ABCInherits fromAbstract MethodsMixin MethodsMappingSized,Iterable,Container__getitem__,__iter__,__len__ __contains__, keys, items, values, get, __eq__, __ne__MutableMappingMapping__getitem__,__setitem__,__delitem__,__iter__,__len__ InheritedMapping methods pop, popitem, clear, update, andsetdefault

Mapping Sized, Iterabel, Container {'__iter__', '__len__', '__contains__'} 96(, , ){'__iter__', '__len__', '__contains__'}

Mapping 97(, , ) {'keys', '__hash__', 'items', 'get', '__eq__', '__getitem__', 'values', '__contains__'}

VIEW 98

VIEW 99ABCInherits fromMixin MethodsMappingViewSized __len__ItemsViewMappingView,Set __contains__, __iter__KeysViewMappingView,Set __contains__, __iter__ValuesViewMappingView __contains__, __iter__

view dict keys, values, items 100

view : View Set MappingView 101(, , ) (,) (, ) (,) (, ) True True

io ABC Moon Yong Joon102

io 103

io ABC io 104ABCInheritsStub MethodsMixin Methods and PropertiesIOBasefileno,seek, andtruncateclose,closed,__enter__,__exit__,flush,isatty,__iter__,__next__,readable,readline,readlines,seekable,tell,writable, andwritelinesRawIOBaseIOBaseReadintoandwriteInheritedIOBasemethods,read,andreadallBufferedIOBaseIOBasedetach,read,read1,andwriteInheritedIOBasemethods,readintoTextIOBaseIOBasedetach,read,readline, andwriteInheritedIOBasemethods,encoding,errors, andnewlines

io ABC io 105

io 106

File : text I/O File io.StringIO 107

File : Binary I/O File io.StringIO binary 108

File : raw I/O File binary buffering 0 109

io.BytesIO 110

io.BytesIO : binary BytesIO class binary 111

io.StringIO 112

io.StringIO : text StringIO class text 113

io.FileIO 114

File mode115ModesDescriptionr . .rb r+ rb+ w . wb . w+ , wb+ . . .a . . , . .ab . . , . .a+ . . . .ab+ . . . .

io.FileIO: binary FileIO class rb mode default 116

file Moon Yong Joon117

File 118

File: text File open TextIOWrapper 119

4.

Moon Yong Joon120

Everything is an object121

122 (object)(object) id (Identity) (object) class type (object) value

, , , 123 ,

NoneType124

NoneType NoneType type class None False 125

NotImplementedType126

NotImplementedType NotImplementedType type class NotImplemented True 127

Ellipsis128

Ellipsis ellipsis type class Ellipsis() True 129

Ellipsis : numpy ellipsis numpy index 130

Callable type 131

Callable types Callable types __call__ 132User-defined functionsInstance methodsGenerator functionsCoroutine functionsBuilt-in functionsBuilt-in methodsClassesClass Instances

Class Instances : __call__ class instance Callable types __call__ 133

/ : __get__ function __get__ 134

Internal types 135

Internal types Internal types 136Internal typesCode objectsFrame objectsTraceback objectsSlice objectsStatic method objectsClass method objects

classmethod/staticmethod 137

{'s': , 'c': , '__module__': '__main__', '__doc__': None, '__dict__': , '__weakref__': }

Number 138

Number Number 139Number int floatcomplex

10 10 numbers 140

2,8,16 2,8,16 int class , numbers 141

numbers

Sequence 142

sequence sequence 143SequenceImmutableSequenceMutableSequence str tuplebytes listbytearray

sequence 1 collections.abc sequence 144

sequence 2 collections.abc sequence 145

Mapping/Set 146

Mapping/set Mapping/set 147MappingSet types dict set frozenset

mapping collections.abc mapping 148

set/frozenset collections.abc set/frozenset 149

5. iteratorgenerator

Moon Yong Joon150

iterator type 151

iterator types iterator types __iter__, __next__ 152Iterator usered defined classGenerator expressionGenerator functions

abc.Iterator class 153(,) (,) ['__abstractmethods__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '_abc_cache', '_abc_negative_cache', '_abc_negative_cache_version', '_abc_registry']

iterator class : Iterator class 154

iterator class : Iterator class 155

generator expression generator iterator 156

generator functions generator iterator 157

Generator type 158

generator types generator types iterator __iter__, __next__ close, send, throw 159Generator expressionGenerator functions

abc.Generator class Iterator Generator close, send, throw 160

(,) ['__abstractmethods__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '_abc_cache', '_abc_negative_cache', '_abc_negative_cache_version', '_abc_registry']

(,) ['__abstractmethods__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '_abc_cache', '_abc_negative_cache', '_abc_negative_cache_version', '_abc_registry', 'close', 'send', 'throw']

generator expression generator generator 161

generator functions generator generator 162

Coroutine 163

collections.abc 164ABCInherits fromAbstract MethodsMixin MethodsAwaitable__await__CoroutineAwaitablesend,throw close

generator vs.coroutineGenerator coroutine send 165

Coroutine : send yield send

Yield send 166

Generator : Generator send 167