Positional vs. Keyword Arguments
In Python, arguments passed to a function can be positional or keyword-based. Positional arguments are assigned to the function's parameters in the order they appear, while keyword arguments are explicitly named and assigned.
Understanding Positional and Keyword Arguments
The text quoted in your question correctly defines positional arguments as those without an equal sign (e.g., width in rectangleArea). Keyword arguments, on the other hand, are followed by an equal sign and an expression that specifies their default value (e.g., height=2).
Example
Consider the following function:
<code class="py">def rectangleArea(width, height): return width * height</code>
In this function, width and height are positional arguments. However, the provided example:
<code class="py">rectangleArea(width=1, height=2)</code>
uses keyword arguments to set the values for width and height.
Confusion Between Argument Types
The text from your question seems to confuse positional and keyword arguments with function parameter defaults. Default values are specified in function definitions, while positional and keyword arguments are used in function calls.
Clarification
In the example above, the function rectangleArea requires two positional arguments, width and height. However, the call to the function passes these arguments using keyword syntax. This is perfectly valid in Python, as the function definition allows for both positional and keyword arguments.
Summary
The above is the detailed content of What\'s the Difference Between Positional and Keyword Arguments in Python?. For more information, please follow other related articles on the PHP Chinese website!