Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark, in light modeAuto light/dark, in dark modeSkip to content
mypy 1.19.1 documentation
Logo
mypy 1.19.1 documentation

First steps

Type system reference

Configuring and running mypy

Miscellaneous

Project Links

Back to top

Built-in types

This chapter introduces some commonly used built-in types. We willcover many other kinds of types later.

Simple types

Here are examples of some common built-in types:

Type

Description

int

integer

float

floating point number

bool

boolean value (subclass ofint)

str

text, sequence of unicode codepoints

bytes

8-bit string, sequence of byte values

object

an arbitrary object (object is the common base class)

All built-in classes can be used as types.

Any type

If you can’t find a good type for some value, you can always fall backtoAny:

Type

Description

Any

dynamically typed value with an arbitrary type

The typeAny is defined in thetyping module.SeeDynamically typed code for more details.

Generic types

In Python 3.9 and later, built-in collection type objects supportindexing:

Type

Description

list[str]

list ofstr objects

tuple[int,int]

tuple of twoint objects (tuple[()] is the empty tuple)

tuple[int,...]

tuple of an arbitrary number ofint objects

dict[str,int]

dictionary fromstr keys toint values

Iterable[int]

iterable object containing ints

Sequence[bool]

sequence of booleans (read-only)

Mapping[str,int]

mapping fromstr keys toint values (read-only)

type[C]

type object ofC (C is a class/type variable/union of types)

The typedict is ageneric class, signified by type arguments within[...]. For example,dict[int,str] is a dictionary from integers tostrings anddict[Any,Any] is a dictionary of dynamically typed(arbitrary) values and keys.list is another generic class.

Iterable,Sequence, andMapping are generic types that correspond toPython protocols. For example, astr object or alist[str] object isvalid whenIterable[str] orSequence[str] is expected.You can import them fromcollections.abc instead of importing fromtyping in Python 3.9.

SeeUsing generic builtins for more details, including how you canuse these in annotations also in Python 3.7 and 3.8.

These legacy types defined intyping are needed if you need to supportPython 3.8 and earlier:

Type

Description

List[str]

list ofstr objects

Tuple[int,int]

tuple of twoint objects (Tuple[()] is the empty tuple)

Tuple[int,...]

tuple of an arbitrary number ofint objects

Dict[str,int]

dictionary fromstr keys toint values

Iterable[int]

iterable object containing ints

Sequence[bool]

sequence of booleans (read-only)

Mapping[str,int]

mapping fromstr keys toint values (read-only)

Type[C]

type object ofC (C is a class/type variable/union of types)

List is an alias for the built-in typelist that supportsindexing (and similarly fordict/Dict andtuple/Tuple).

Note that even thoughIterable,Sequence andMapping looksimilar to abstract base classes defined incollections.abc(formerlycollections), they are not identical, since the latterdon’t support indexing prior to Python 3.9.

On this page

[8]ページ先頭

©2009-2025 Movatter.jp