args cho phép nhận bất kỳ số positional arguments — nhóm thành tuple. *kwargs nhận bất kỳ số keyword arguments — nhóm thành dict.
python
def log(level, *args, **kwargs):
print(f"[{level}]", *args)
for k, v in kwargs.items():
print(f" {k}: {v}")
log("INFO", "Server started", port=8080, debug=True)
# [INFO] Server started
# port: 8080
# debug: True
# Unpacking khi gọi function
def create_user(name, email, age):
...
data = {"name": "Alice", "email": "a@b.com", "age": 25}
create_user(**data) # Unpack dict thành kwargsThứ tự params: def fn(pos, /, normal, args, kw_only, *kwargs).
Lưu ý: args và *kwargs không giữ type information — dùng overloads hoặc TypedDict cho strict typing.
args accepts any number of positional arguments — groups them into a tuple. *kwargs accepts any number of keyword arguments — groups them into a dict.
python
def greet(*names, greeting="Hello"):
for name in names:
print(f"{greeting}, {name}!")
greet("Alice", "Bob", greeting="Hi")
# Unpacking
def point(x, y, z): ...
coords = (1, 2, 3)
point(*coords) # Unpack tupleOrder of params: def fn(positional, /, normal, args, keyword_only, *kwargs).
Pitfall: args and *kwargs lose type info — use overloads or TypedDict for strict typing.