Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python Positional-Only Arguments (Complete Guide for Beginners)

In Python, positional-only arguments are a special type of function parameter that must be passed using position, not by name (keyword).

This feature helps you write safer and more controlled APIs where argument order is strictly required.


🔹 What are Positional-Only Arguments?

Positional-only arguments are parameters that:

Can ONLY be passed based on their position, NOT by name=value

They are defined using a special symbol:

👉 / (forward slash)


🔹 Syntax of Positional-Only Arguments

def function_name(param1, param2, /):
pass

🔍 Rule:

  • Everything before / is positional-only
  • You cannot use keywords for them

🔹 Simple Example of Positional-Only Arguments

def student(name, age, /):
print("Name:", name)
print("Age:", age)

student("Alex", 20)

🔸 Output:

Name: Alex
Age: 20

🔹 ❌ Wrong Usage (Keyword Not Allowed)

def student(name, age, /):
print(name, age)

student(name="Alex", age=20)

❌ This will cause an error

🔍 Why?

  • Parameters before / cannot be used as keywords

🔹 How Positional-Only Arguments Work

Function Definition:
(name, age, /)

Call:
("Alex", 20)

Mapping:
name → "Alex"
age → 20

🔹 Example: Calculator Function

def add(a, b, /):
return a + b

print(add(10, 5))

🔸 Output:

15

🔹 Example: Product System

def product(name, price, /):
print("Product:", name)
print("Price:", price)

product("Laptop", 1200)

🔸 Output:

Product: Laptop
Price: 1200

🔹 Mixing Positional-Only and Other Types

Python allows combining different argument types:

def example(a, b, /, c, d):
print(a, b, c, d)

example(10, 20, c=30, d=40)

🔸 Output:

10 20 30 40

🔹 Explanation of Structure

a, b  → positional-only (before /)
c, d → can be keyword or positional (after /)

🔹 ❌ Invalid Mixing Example

def demo(a, b, /, c, d):
print(a, b, c, d)

demo(a=10, b=20, c=30, d=40)

❌ Error occurs because:

  • a and b must NOT use keywords

🔹 Real-Life Example: Bank System

def transfer(amount, account, /, currency="USD"):
print("Amount:", amount)
print("Account:", account)
print("Currency:", currency)

transfer(500, "ACC123")
transfer(1000, "ACC456", currency="KHR")

🔸 Output:

Amount: 500
Account: ACC123
Currency: USD

Amount: 1000
Account: ACC456
Currency: KHR

🔹 Real-Life Example: Image Processing Function

def resize(width, height, /, format="png"):
print("Width:", width)
print("Height:", height)
print("Format:", format)

resize(1920, 1080)
resize(800, 600, format="jpg")

🔹 Why Use Positional-Only Arguments?

✔ Prevents misuse of parameter names
✔ Improves function performance
✔ Useful for internal APIs
✔ Keeps interface clean and strict
✔ Avoids breaking changes in large projects


🔹 Positional-Only vs Other Argument Types

TypeSymbolCan use keyword?Example
Positional-only     /           ❌ No      func(10, 20)
Positional/Keyword         none           ✅ Yes      func(a=10)
Keyword-only     *           ✅ Yes (required)      func(a=10)

🔹 Full Function Combination Example

def demo(a, b, /, c, *, d):
print(a, b, c, d)

demo(10, 20, c=30, d=40)

🔍 Explanation:

  • a, b → positional-only
  • c → positional or keyword
  • d → keyword-only

🚀 Conclusion

Python positional-only arguments give developers more control over how functions are used. They ensure that important parameters are passed in the correct order and prevent accidental misuse.

They are commonly used in:

  • Built-in Python functions
  • Performance-critical code
  • Library and framework design
  • Professional APIs 

 



Post a Comment

0 Comments