Hackforge Academy

Category: python

Positional-only Parameters

Published on 27 Oct 2026

Explanation

Parameters before / are positional-only and cannot be supplied by keyword.

Code:

def divide(a, b, /): return a / b
print(divide(10, 2))

Explanation

Parameters after a standalone * must be passed by keyword.

Code:

def connect(host, *, timeout=30): print(host, timeout)
connect('localhost', timeout=10)

Explanation

A signature can combine positional-only, regular, and keyword-only parameters.

Code:

def create_user(name, /, role, *, active=True):
    print(name, role, active)
create_user('Arun', 'Developer', active=False)

Explanation

Keyword-only arguments make optional settings explicit and reduce mistakes caused by argument order.

Code:

def fetch(url, *, timeout=10, retries=3):
    print(url, timeout, retries)
fetch('https://example.com', timeout=5)

Explanation

Use parameter restrictions when they make an API easier to understand or maintain.

Code:

def price(amount, quantity, /, *, tax=0.0):
    return amount * quantity * (1 + tax)
print(price(100, 2, tax=0.18))

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community