Teks sumber — English
Terjemahan — Bahasa Indonesia
The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.
Interpreter Python memiliki sejumlah fungsi dan tipe bawaan di dalamnya yang selalu tersedia. Mereka terdaftar di sini dalam urutan abjad.
:func:`type`
:func:`type`
Return a Boolean value, i.e. one of ``True`` or ``False``. The argument is converted using the standard :ref:`truth testing procedure <truth>`. If the argument is false or omitted, this returns ``False``; otherwise, it returns ``True``. The :class:`bool` class is a subclass of :class:`int` (see :ref:`typesnumeric`). It cannot be subclassed further. Its only instances are ``False`` and ``True`` (see :ref:`typebool`).
This function drops you into the debugger at the call site. Specifically, it calls :func:`sys.breakpointhook`, passing ``args`` and ``kws`` straight through. By default, ``sys.breakpointhook()`` calls :func:`pdb.set_trace` expecting no arguments. In this case, it is purely a convenience function so you don't have to explicitly import :mod:`pdb` or type as much code to enter the debugger. However, :func:`sys.breakpointhook` can be set to some other function and :func:`breakpoint` will automatically call that, allowing you to drop into the debugger of choice. If :func:`sys.breakpointhook` is not accessible, this function will raise :exc:`RuntimeError`.
Return a new array of bytes. The :class:`bytearray` class is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of mutable sequences, described in :ref:`typesseq-mutable`, as well as most methods that the :class:`bytes` type has, see :ref:`bytes-methods`.
Kembalikan array byte baru. Kelas :class:`bytearray` adalah urutan bilangan bulat yang dapat berubah dalam kisaran 0 <= x <256. Ia memiliki sebagian besar metode urutan-urutan yang dapat berubah, yang dijelaskan dalam :ref:`typesseq-mutable`, dan juga sebagian besar metode yang dimiliki oleh :class:`bytes`, lihat :ref:`bytes-methods`.
See also :ref:`binaryseq` and :ref:`typebytearray`.
Lihat juga :ref:`binaryseq` dan :ref:`typebytearray`.
See also :ref:`binaryseq`, :ref:`typebytes`, and :ref:`bytes-methods`.
Lihat juga :ref:`binaryseq`, :ref:`typebytes`, dan :ref:`bytes-methods`.
Class methods are different than C++ or Java static methods. If you want those, see :func:`staticmethod` in this section. For more information on class methods, see :ref:`types`.
The *mode* argument specifies what kind of code must be compiled; it can be ``'exec'`` if *source* consists of a sequence of statements, ``'eval'`` if it consists of a single expression, or ``'single'`` if it consists of a single interactive statement (in the latter case, expression statements that evaluate to something other than ``None`` will be printed). It can also be ``'func_type'`` if *source* consists of a function type signature, for example ``(int) -> str``; see :func:`ast.parse` for more information. This mode requires passing :data:`ast.PyCF_ONLY_AST` in *flags*.
Previously, :exc:`TypeError` was raised when null bytes were encountered in *source*.
Sebelumnya, :exc:`TypeError` dimunculkan ketika byte *null* ditemui di *source*.
Added support for ``mode='func_type'``.
If two arguments are provided or keyword arguments are used, each argument may be any numeric type (including complex). If both arguments are real numbers, return a complex number with the real component *real* and the imaginary component *imag*. If both arguments are complex numbers, return a complex number with the real component ``real.real-imag.imag`` and the imaginary component ``real.imag+imag.real``. If one of arguments is a real number, only its real component is used in the above expressions.
The complex type is described in :ref:`typesnumeric`.
Tipe kompleks dijelaskan dalam :ref:`typesnumeric`.
Create a new dictionary. The :class:`dict` object is the dictionary class. See also :ref:`typesmapping` for documentation about this class.
If the object does not provide :meth:`~object.__dir__`, the function tries its best to gather information from the object's :attr:`~object.__dict__` attribute, if defined, and from its type object. The resulting list is not necessarily complete and may be inaccurate when the object has a custom :func:`~object.__getattr__`.
The default :func:`dir` mechanism behaves differently with different types of objects, as it attempts to produce the most relevant, rather than complete, information:
Mekanisme bawaan :func:`dir` berperilaku berbeda dengan berbagai jenis objek, karena berusaha menghasilkan informasi yang paling relevan, dibanding lengkap,:
If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.
Jika objek adalah tipe atau objek kelas, daftar berisi nama atributnya, dan secara rekursif atribut dari basisnya.
Take two (non-complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using integer division. With mixed operand types, the rules for binary arithmetic operators apply. For integers, the result is the same as ``(a // b, a % b)``. For floating-point numbers the result is ``(q, a % b)``, where *q* is usually ``math.floor(a / b)`` but may be 1 less than that. In any case ``q * b + a % b`` is very close to *a*, if ``a % b`` is non-zero it has the same sign as *b*, and ``0 <= abs(a % b) < abs(b)``.
The float type is described in :ref:`typesnumeric`.
Tipe float dijelaskan dalam :ref:`typesnumeric`.
Convert a *value* to a "formatted" representation, as controlled by *format_spec*. The interpretation of *format_spec* will depend on the type of the *value* argument; however, there is a standard formatting syntax that is used by most built-in types: :ref:`formatspec`.
A call to ``format(value, format_spec)`` is translated to ``type(value).__format__(value, format_spec)`` which bypasses the instance dictionary when searching for the value's :meth:`~object.__format__` method. A :exc:`TypeError` exception is raised if the method search reaches :mod:`object` and the *format_spec* is non-empty, or if either the *format_spec* or the return value are not strings.
``object().__format__(format_spec)`` raises :exc:`TypeError` if *format_spec* is not an empty string.
``object().__format__(format_spec)`` menimbulkan :exc:`TypeError` jika *format_spec* bukan string kosong.
Create a new frozen dictionary. The :class:`frozendict` object is a built-in class. See also :ref:`typesmapping` for documentation about this class.
Return a new :class:`frozenset` object, optionally with elements taken from *iterable*. :class:`frozenset` is a built-in class. See also :ref:`types-set` for documentation about this class.
Return the hash value of the object (if it has one). Hash values are integers. They are used to quickly compare dictionary keys during a dictionary lookup. Numeric values that compare equal have the same hash value (even if they are of different types, as is the case for 1 and 1.0).
Kembalikan nilai hash objek (jika ada). Nilai hash adalah bilangan bulat. Mereka digunakan untuk dengan cepat membandingkan kunci kamus *dictionary keys* selama pencarian dictionary. Nilai numerik yang membandingkan sama memiliki nilai hash yang sama (bahkan jika mereka dari jenis yang berbeda, seperti halnya untuk 1 dan 1.0).
The integer type is described in :ref:`typesnumeric`.
Tipe integer dijelaskan dalam :ref:`typesnumeric`.
Return ``True`` if the *object* argument is an instance of the *classinfo* argument, or of a (direct, indirect, or :term:`virtual <abstract base class>`) subclass thereof. If *object* is not an object of the given type, the function always returns ``False``. If *classinfo* is a tuple of type objects (or recursively, other such tuples) or a :ref:`types-union` of multiple types, return ``True`` if *object* is an instance of any of the types. If *classinfo* is not a type or tuple of types and such tuples, a :exc:`TypeError` exception is raised. :exc:`TypeError` may not be raised for an invalid type if an earlier check succeeds.
*classinfo* can be a :ref:`types-union`.
Return ``True`` if *cls* is a subclass (direct, indirect, or :term:`virtual <abstract base class>`) of *classinfo*. A class is considered a subclass of itself. *classinfo* may be a tuple of class objects (or recursively, other such tuples) or a :ref:`types-union`, in which case return ``True`` if *cls* is a subclass of any entry in *classinfo*. In any other case, a :exc:`TypeError` exception is raised.
Return an :term:`iterator` object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second argument, the single argument must be a collection object which supports the :term:`iterable` protocol (the :meth:`~object.__iter__` method), or it must support the sequence protocol (the :meth:`~object.__getitem__` method with integer arguments starting at ``0``). If it does not support either of those protocols, :exc:`TypeError` is raised. If the second argument, *sentinel*, is given, then the first argument must be a callable object. The iterator created in this case will call *callable* with no arguments for each call to its :meth:`~iterator.__next__` method; if the value returned is equal to *sentinel*, :exc:`StopIteration` will be raised, otherwise the value will be returned.
See also :ref:`typeiter`.
Lihat juga :ref:`typeiter`.
Rather than being a function, :class:`list` is actually a mutable sequence type, as documented in :ref:`typesseq-list` and :ref:`typesseq`.
Alih-alih menjadi sebuah fungsi, :class:`list` sebenarnya adalah tipe urutan yang bisa berubah *mutable*, seperti yang didokumentasikan dalam :ref:`typesseq-list` dan :ref:`typesseq`.
Return a "memory view" object created from the given argument. See :ref:`typememoryview` for more information.
Kembalikan objek "memory view" yang dibuat dari argumen yang diberikan. Lihat :ref:`typememoryview` untuk informasi lebih lanjut.
The type of :term:`file object` returned by the :func:`open` function depends on the mode. When :func:`open` is used to open a file in a text mode (``'w'``, ``'r'``, ``'wt'``, ``'rt'``, etc.), it returns a subclass of :class:`io.TextIOBase` (specifically :class:`io.TextIOWrapper`). When used to open a file in a binary mode with buffering, the returned class is a subclass of :class:`io.BufferedIOBase`. The exact class varies: in read binary mode, it returns an :class:`io.BufferedReader`; in write binary and append binary modes, it returns an :class:`io.BufferedWriter`, and in read/write mode, it returns an :class:`io.BufferedRandom`. When buffering is disabled, the raw stream, a subclass of :class:`io.RawIOBase`, :class:`io.FileIO`, is returned.
Tipe :term:`file object` yang dikembalikan oleh fungsi :func:`open` tergantung pada mode. Ketika :func:`open` digunakan untuk membuka berkas dalam mode teks (``'w'``, ``'r'``, ``'wt'``, ``'rt'``, dll.), ia mengembalikan subkelas dari :class:`io.TextIOBase` (khusus :class:`io.TextIOWrapper`). Ketika digunakan untuk membuka file dalam mode biner dengan *buffering*, kelas yang dikembalikan adalah subkelas dari :class:`io.BufferedIOBase`. Kelas yang tepat bervariasi: dalam mode baca biner, ia mengembalikan :class:`io.BufferedReader`; dalam mode tulis biner dan *append* biner, ia mengembalikan sebuah :class:`io.BufferedWriter`, dan dalam mode baca/tulis, ia mengembalikan sebuah :class:`io.BufferedRandom`. Ketika *buffering* dinonaktifkan, aliran tak diproses *raw stream*, subkelas dari :class:`io.RawIOBase`, :class:`io.FileIO`, dikembalikan.
When arguments are builtin numeric types with mixed operand types, the coercion rules for binary arithmetic operators apply. For :class:`int` operands, the result has the same type as the operands (after coercion) unless the second argument is negative; in that case, all arguments are converted to float and a float result is delivered. For example, ``pow(10, 2)`` returns ``100``, but ``pow(10, -2)`` returns ``0.01``. For a negative base of type :class:`int` or :class:`float` and a non-integral exponent, a complex result is delivered. For example, ``pow(-9, 0.5)`` returns a value close to ``3j``. Whereas, for a negative base of type :class:`int` or :class:`float` with an integral exponent, a float result is delivered. For example, ``pow(-9, 2.0)`` returns ``81.0``.
For :class:`int` operands *base* and *exp*, if *mod* is present, *mod* must also be of integer type and *mod* must be nonzero. If *mod* is present and *exp* is negative, *base* must be relatively prime to *mod*. In that case, ``pow(inv_base, -exp, mod)`` is returned, where *inv_base* is an inverse to *base* modulo *mod*.
Untuk operan :class:`int` *base* dan *exp*, jika *mod* ada, *mod* juga harus bertipe integer dan *mod* harus bukan nol. Jika *mod* ada dan *exp* negatif, *basis* harus relatif prima ke *mod*. Dalam kasus itu, ``pow(inv_base, -exp, mod)`` dikembalikan, di mana *inv_base* adalah kebalikan dari *base* *modulo* *mod*.
Rather than being a function, :class:`range` is actually an immutable sequence type, as documented in :ref:`typesseq-range` and :ref:`typesseq`.
Alih-alih menjadi fungsi, :class:`range` sebenarnya merupakan tipe urutan yang tidak dapat diubah *immutable*, seperti yang didokumentasikan dalam :ref:`typesseq-range` dan :ref:`typesseq`.
Return a string containing a printable representation of an object. For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to :func:`eval`; otherwise, the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a :meth:`~object.__repr__` method.
For the built-in types supporting :func:`round`, values are rounded to the closest multiple of 10 to the power minus *ndigits*; if two multiples are equally close, rounding is done toward the even choice (so, for example, both ``round(0.5)`` and ``round(-0.5)`` are ``0``, and ``round(1.5)`` is ``2``). Any integer value is valid for *ndigits* (positive, zero, or negative). The return value is an integer if *ndigits* is omitted or ``None``. Otherwise, the return value has the same type as *number*.
Return a new :class:`set` object, optionally with elements taken from *iterable*. :class:`set` is a built-in class. See also :ref:`types-set` for documentation about this class.
Sentinels are conventionally assigned to a variable with a matching name. Sentinels defined in this way can be used in :term:`type hints <type hint>`::
Sentinel objects support the :ref:`| <bitwise>` operator for use in type expressions.
The sort algorithm uses only ``<`` comparisons between items. While defining an :meth:`~object.__lt__` method will suffice for sorting, :PEP:`8` recommends that all six :ref:`rich comparisons <comparisons>` be implemented. This will help avoid bugs when using the same data with other ordering tools such as :func:`max` that rely on a different underlying method. Implementing all six comparisons also helps avoid confusion for mixed type comparisons which can call the reflected :meth:`~object.__gt__` method.
For more information on static methods, see :ref:`types`.
Untuk informasi lebih lanjut tentang metode statis, lihat :ref:`types`.
Return a proxy object that delegates method calls to a parent or sibling class of *type*. This is useful for accessing inherited methods that have been overridden in a class.
Kembalikan objek proxy yang mendelegasikan panggilan metode ke kelas orang tua atau saudara dari *type*. Ini berguna untuk mengakses metode yang diwariskan yang telah ditimpa di suatu kelas.
The *object_or_type* determines the :term:`method resolution order` to be searched. The search starts from the class right after the *type*.
For example, if :attr:`~type.__mro__` of *object_or_type* is ``D -> B -> C -> A -> object`` and the value of *type* is ``B``, then :func:`super` searches ``C -> A -> object``.
The :attr:`~type.__mro__` attribute of the class corresponding to *object_or_type* lists the method resolution search order used by both :func:`getattr` and :func:`super`. The attribute is dynamic and can change whenever the inheritance hierarchy is updated.
If the second argument is omitted, the super object returned is unbound. If the second argument is an object, ``isinstance(obj, type)`` must be true. If the second argument is a type, ``issubclass(type2, type)`` must be true (this is useful for classmethods).
Jika argumen kedua dihilangkan, objek super yang dikembalikan tidak terikat *unbound*. Jika argumen kedua adalah objek, ``isinstance(obj, type)`` harus benar. Jika argumen kedua adalah tipe, ``issubclass(type2, type)`` harus benar (ini berguna untuk metode-metode kelas).
When called directly within an ordinary method of a class, both arguments may be omitted ("zero-argument :func:`!super`"). In this case, *type* will be the enclosing class, and *object_or_type* will be the first argument of the immediately enclosing function (typically ``self``). Zero-argument :func:`!super` will not work as expected within nested functions, including generator expressions, which implicitly create nested functions.
Rather than being a function, :class:`tuple` is actually an immutable sequence type, as documented in :ref:`typesseq-tuple` and :ref:`typesseq`.
Alih-alih menjadi fungsi, :class:`tuple` sebenarnya merupakan tipe urutan yang tidak dapat diubah atau disebut *immutable*, seperti yang didokumentasikan dalam :ref:`typesseq-tuple` dan :ref:`typesseq`.
With one argument, return the type of an *object*. The return value is a type object and generally the same object as returned by :attr:`object.__class__`.
The :func:`isinstance` built-in function is recommended for testing the type of an object, because it takes subclasses into account.
Fungsi bawaan :func:`isinstance` direkomendasikan untuk menguji jenis objek, karena ia memperhitungkan subkelas.
With three arguments, return a new type object. This is essentially a dynamic form of the :keyword:`class` statement. The *name* string is the class name and becomes the :attr:`~type.__name__` attribute. The *bases* tuple contains the base classes and becomes the :attr:`~type.__bases__` attribute; if empty, :class:`object`, the ultimate base of all classes, is added. The *dict* dictionary contains attribute and method definitions for the class body; it may be copied or wrapped before becoming the :attr:`~type.__dict__` attribute. The following two statements create identical :class:`!type` objects:
:ref:`bltin-type-objects`
Unlike a :keyword:`class` statement, the three argument form does not call the metaclass ``__prepare__`` method (see :ref:`prepare`). Use :func:`types.new_class` to dynamically create a class using the appropriate metaclass.
Subclasses of :class:`!type` which don't override ``type.__new__`` may no longer use the one-argument form to get the type of an object.
Objects such as modules and instances have an updateable :attr:`~object.__dict__` attribute; however, other objects may have write restrictions on their :attr:`!__dict__` attributes (for example, classes use a :class:`types.MappingProxyType` to prevent direct dictionary updates).
A :exc:`TypeError` exception is raised if an object is specified but it doesn't have a :attr:`~object.__dict__` attribute (for example, if its class defines the :attr:`~object.__slots__` attribute).
Sebuah pengecualian untuk :exc:`TypeError` akan muncul jika suatu objek yang ditentukan tetapi tidak memiliki sebuah atribut :attr:`~object.__dict__` (contohnya, jika suatu kelas didefinisikan sebagai atribut :attr:`~object.__slots__`).
type
tipe