3.7からdataclass
なるものが実装された。NULL安全だけでなくTYPE安全も考慮されつつある? 型アノテーションの重要性が今後ますます高まりそう。なのでPythonの型について調査することにした。
成果物
情報源
型
組み込み型の一覧はない。ドキュメントから抽出するに以下のとおり。
typing, collectionsモジュールに含まれたメンバは次のとおり。この中から型(class
)
>>> import typing >>> dir(typing) ['AbstractSet', 'Any', 'AnyStr', 'AsyncContextManager', 'AsyncGenerator', 'AsyncIterable', 'AsyncIterator', 'Awaitable', 'BinaryIO', 'ByteString', 'CT_co', 'Callable', 'ChainMap', 'ClassVar', 'Collection', 'Container', 'ContextManager', 'Coroutine', 'Counter', 'DefaultDict', 'Deque', 'Dict', 'ForwardRef', 'FrozenSet', 'Generator', 'Generic', 'Hashable', 'IO', 'ItemsView', 'Iterable', 'Iterator', 'KT', 'KeysView', 'List', 'Mapping', 'MappingView', 'Match', 'MethodDescriptorType', 'MethodWrapperType', 'MutableMapping', 'MutableSequence', 'MutableSet', 'NamedTuple', 'NamedTupleMeta', 'NewType', 'NoReturn', 'Optional', 'OrderedDict', 'Pattern', 'Reversible', 'Sequence', 'Set', 'Sized', 'SupportsAbs', 'SupportsBytes', 'SupportsComplex', 'SupportsFloat', 'SupportsInt', 'SupportsRound', 'T', 'TYPE_CHECKING', 'T_co', 'T_contra', 'Text', 'TextIO', 'Tuple', 'Type', 'TypeVar', 'Union', 'VT', 'VT_co', 'V_co', 'ValuesView', 'WrapperDescriptorType', '_Final', '_GenericAlias', '_Immutable', '_Protocol', '_ProtocolMeta', '_SpecialForm', '_TypingEllipsis', '_TypingEmpty', '_VariadicGenericAlias', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_alias', '_allowed_types', '_check_generic', '_cleanups', '_collect_type_vars', '_eval_type', '_get_defaults', '_is_dunder', '_make_nmtuple', '_normalize_alias', '_overload_dummy', '_prohibited', '_remove_dups_flatten', '_special', '_subs_tvars', '_tp_cache', '_type_check', '_type_repr', 'abc', 'abstractmethod', 'abstractproperty', 'cast', 'collections', 'contextlib', 'functools', 'get_type_hints', 'io', 'no_type_check', 'no_type_check_decorator', 'operator', 'overload', 're', 'stdlib_re', 'sys', 'types']
>>> import collections >>> dir(collections) ['ChainMap', 'Counter', 'OrderedDict', 'UserDict', 'UserList', 'UserString', '_Link', '_OrderedDictItemsView', '_OrderedDictKeysView', '_OrderedDictValuesView', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__getattr__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_chain', '_collections_abc', '_count_elements', '_eq', '_heapq', '_iskeyword', '_itemgetter', '_nt_itemgetters', '_proxy', '_recursive_repr', '_repeat', '_starmap', '_sys', 'abc', 'defaultdict', 'deque', 'namedtuple']
ほかにもPathなど多数のクラス型がある。また、UrlやFileなどクラス型ではないオブジェクトもある。どう使うかはよくよくPythonライブラリの仕様を理解する必要がありそうだ。
所感
対象環境
- Raspbierry pi 4 Model B
- Raspberry Pi OS buster 10.0 2020-08-20 ※
- bash 5.0.3(1)-release
$ uname -a Linux raspberrypi 5.4.83-v7l+ #1379 SMP Mon Dec 14 13:11:54 GMT 2020 armv7l GNU/Linux