martes, 23 de diciembre de 2025

function parameters

 # 1️⃣ *args — Extra positional parameters

def print_numbers(*args):

    print("Positional numbers:", args)


print_numbers(1, 2, 3, 4)

# Output:

# Positional numbers: (1, 2, 3, 4)

print()


# 2️⃣ **kwargs — Extra keyword parameters

def print_info(**kwargs):

    print("Keyword info:", kwargs)


print_info(name="Alice", age=17, country="FR")

# Output:

# Keyword info: {'name': 'Alice', 'age': 17, 'country': 'FR'}

print()


# 3️⃣ * — Force keyword-only arguments

def connect(host, port, *, timeout=5, ssl=False):

    print(host, port, timeout, ssl)


connect("localhost", 5432, timeout=10, ssl=True)

# Output:

# localhost 5432 10 True

print()


# 4️⃣ ** in a call — Unpack dict into keywords

def greet(name, age):

    print(f"Hello {name}, you are {age} years old!")


data = {"name": "Bob", "age": 20}

greet(**data)

# Output:

# Hello Bob, you are 20 years old!

print()


# 5️⃣ Combined example with all of them

def full_example(a, b, *args, c=10, **kwargs):

    print("a, b:", a, b)

    print("args:", args)

    print("c:", c)

    print("kwargs:", kwargs)


full_example(1, 2, 3, 4, 5, c=99, x=100, y=200)

# Output:

# a, b: 1 2

# args: (3, 4, 5)

# c: 99

# kwargs: {'x': 100, 'y': 200}


No hay comentarios:

Publicar un comentario