Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python - Positional-Only Arguments



Positional Only Arguments

It is possible inPython to define afunction in which one or more arguments can not accept their value with keywords. Such arguments are calledpositional-only arguments.

To make an argument positional-only, use the forward slash (/) symbol. All the arguments before this symbol will be treated as positional-only.

Python'sbuilt-in input() function is an example of positional-only arguments. The syntax of input function is −

input(prompt = "")

Prompt is an explanatory string for the benefit of the user. However, you cannot use the prompt keyword inside the parentheses.

Example

In this example, we are usingprompt keyword, which will lead to error.

name = input(prompt="Enter your name ")

On executing, this code will show the following error message −

   name = input (prompt="Enter your name ")         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^TypeError: input() takes no keyword arguments

Positional-Only Arguments Examples

Let's understand positional-only arguments with the help of some examples −

Example 1

In this example, we make both the arguments ofintr() function as positional-only by putting "/" at the end.

def intr(amt, rate, /):   val = amt * rate / 100   return val   print(intr(316200, 4))

When you run the code, it will show the following result −

12648.0

Example 2

If we try to use the arguments as keywords, Python raises errors as shown in the below example.

def intr(amt, rate, /):   val = amt * rate / 100   return val   print(intr(amt=1000, rate=10))

On running this code, it will show following error message −

   interest = intr(amt=1000, rate=10)              ^^^^^^^^^^^^^^^^^^^^^^^TypeError: intr() got some positional-only arguments passed as keyword arguments: 'amt, rate'

Example 3

A function may be defined in such a way that it has some keyword-only and some positional-only arguments. Here,x is a required positional-only argument,y is a regular positional argument, andz is a keyword-only argument.

def myfunction(x, /, y, *, z):   print (x, y, z)   myfunction(10, y=20, z=30)myfunction(10, 20, z=30)

The above code will show the following output −

10 20 3010 20 30
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp