Pelokalan
Python ID
id en
Masuk
Proyek
faq__programming.po Berkas proyek · python-docs-id-3.14 · Bahasa Indonesia
Modul PO Hanya lihat
Teks sumber — English
Terjemahan — Bahasa Indonesia
Because of this feature, it is good programming practice to not use mutable objects as default values. Instead, use ``None`` as the default value and inside the function, check if the parameter is ``None`` and create a new list/dictionary/whatever if it is. For example, don't write::
This feature can be useful. When you have a function that's time-consuming to compute, a common technique is to cache the parameters and the resulting value of each call to the function, and return the cached value if the same value is requested again. This is called "memoizing", and can be implemented like this::
# Callers can only provide two parameters and optionally pass _cache by keyword def expensive(arg1, arg2, *, _cache={}): if (arg1, arg2) in _cache: return _cache[(arg1, arg2)] # Calculate the value result = ... expensive computation ... _cache[(arg1, arg2)] = result # Store result in the cache return result
How can I pass optional or keyword parameters from one function to another?
Collect the arguments using the ``*`` and ``**`` specifiers in the function's parameter list; this gives you the positional arguments as a tuple and the keyword arguments as a dictionary. You can then pass these arguments when calling another function by using ``*`` and ``**``::
What is the difference between arguments and parameters?
:term:`Parameters <parameter>` are defined by the names that appear in a function definition, whereas :term:`arguments <argument>` are the values actually passed to a function when calling it. Parameters define what :term:`kind of arguments <parameter>` a function can accept. For example, given the function definition::
*foo*, *bar* and *kwargs* are parameters of ``func``. However, when calling ``func``, for example::
How do I write a function with output parameters (call by reference)?
What does the slash(/) in the parameter list of a function mean?
A slash in the argument list of a function denotes that the parameters prior to it are positional-only. Positional-only parameters are the ones without an externally usable name. Upon calling a function that accepts positional-only parameters, arguments are mapped to parameters based solely on their position. For example, :func:`divmod` is a function that accepts positional-only parameters. Its documentation looks like this::
The slash at the end of the parameter list means that both parameters are positional-only. Thus, calling :func:`divmod` with keyword arguments would lead to an error::
For simple input parsing, the easiest approach is usually to split the line into whitespace-delimited words using the :meth:`~str.split` method of string objects and then convert decimal strings to numeric values using :func:`int` or :func:`float`. :meth:`!split` supports an optional "sep" parameter which is useful if the line uses something other than whitespace as a separator.
class C: @staticmethod def static(arg1, arg2, arg3): # No 'self' parameter! ...
This will write the ``.pyc`` to a ``__pycache__`` subdirectory in the same location as ``foo.py`` (or you can override that with the optional parameter *cfile*).
difference from parameter
parameter
parameter