Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Modern Python offers precise control over how callers invoke your functions. In Python's official documentation (Section 4.8.3 "Special parameters"), you will encounter function signatures using / (slash) and *`` (asterisk), as well as Type Annotations**.
Let's demystify these modern Python features step by step!
A complete Python function signature can look like this:
/ must be passed by position only./ and ``** can be passed by position OR keyword./ (PEP 570)When you don't want callers using parameter names (e.g., when the parameter name might change or has no semantic meaning to the caller), use /:
Many built-in Python functions work this way (e.g., len([1, 2, 3]) cannot be called as len(obj=[1, 2, 3])).
* (PEP 3102)When an argument configures behavior or is easy to mix up (such as booleans or options), forcing keyword invocation makes caller code self-documenting:
Python is dynamically typed, but adding Type Hints allows editors like VS Code to catch typos, provide intelligent auto-completion, and document your code:
base_price: float indicates the expected input type.-> float documents the return type.mypy or IDE linters.def add(a, b, /):, how must the arguments a and b be provided?def register(name, *, role='student'):, what does the * indicate?def get_status() -> str:?def test(x: str):?def query(endpoint, /, *, timeout=30):?In the next lesson, we will apply our function mastery to build Project: Calculator (14: Functions).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Scope of Variables in Functions | Project: Calculator |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.