Skip to content
Home / Fundamentals

Python Function Arguments

No Arguments, Number of Arguments

In Python, you can define a function that takes no arguments by simply not including any parameters in the function definition. For example:

def greet():
    print("Hello, world!")

greet()  # prints "Hello, world!"

You can also define a function that takes a specific number of arguments by including that number of parameters in the function definition. For example:

def greet(name):
    print(f"Hello, {name}!")

greet("Alice")  # prints "Hello, Alice!"
greet("Bob")  # prints "Hello, Bob!"

# This will raise a TypeError because the function expects one argument
greet()

Best Practices

  • When defining a function that takes no arguments, make sure that the function does not depend on any external variables or state. This will make the function more reusable and easier to understand.
  • When defining a function that takes a specific number of arguments, consider using descriptive names for the parameters to make it clear what each argument represents.

Default Arguments

Sometimes, it is useful to specify default values for function arguments. This allows you to call the function with fewer arguments, and the default values will be used for any arguments that you do not specify.

To specify default values for function arguments, you can use the = operator in the function definition. For example:

def greet(name="world"):
    print(f"Hello, {name}!")

greet()  # prints "Hello, world!"
greet("Alice")  # prints "Hello, Alice!"

Note that default arguments are only used when the corresponding parameter is not passed in the function call. For example:

def greet(greeting="Hello", name="world"):
    print(f"{greeting}, {name}!")

greet()  # prints "Hello, world!"
greet("Hi")  # prints "Hi, world!"
greet("Hi", "Alice")  # prints "Hi, Alice!"

Best Practices

  • Use default arguments sparingly, as they can make the function harder to understand if there are too many of them.
  • Consider using default arguments only for optional parameters that are not expected to change frequently.
  • When defining a function with default arguments, put the arguments with default values at the end of the parameter list. This makes it easier to see which arguments are required and which are optional when calling the function.

*args

Sometimes, you may want to define a function that can take an arbitrary number of arguments. You can do this using the *args syntax in the function definition.

The *args syntax allows you to pass a variable number of arguments to a function as a tuple. For example:

def sum_numbers(*args):
    total = 0
    for number in args:
        total += number
    return total

print(sum_numbers(1, 2, 3))  # prints 6
print(sum_numbers(1, 2, 3, 4, 5))  # prints 15
print(sum_numbers())  # prints 0

Best Practices

  • When using *args, keep in mind that the arguments will be passed to the function as a tuple. This means that you will not be able to modify the arguments within the function.
  • Consider using *args only when you need to accept a variable number of arguments that do not have a specific meaning or purpose.

**kwargs

Similarly, you can define a function that takes a variable number of keyword arguments using the **kwargs syntax.

The **kwargs syntax allows you to pass a variable number of keyword arguments to a function as a dictionary. For example:

def print_keyword_arguments(**kwargs):
    for key, value in kwargs.items():
        print(f"{key} = {value}")

print_keyword_arguments(name="Alice", age=30)
# prints "name = Alice" and "age = 30"

print_keyword_arguments(name="Bob", age=35, location="New York")
# prints "name = Bob", "age = 35
prints "location = New York"
print_keyword_arguments() # prints nothing

You can also use the * and ** syntax when calling a function to unpack a tuple or dictionary into separate arguments. For example:

def greet(greeting, name):
    print(f"{greeting}, {name}!")

# This will raise a TypeError because the function expects two arguments
greet("Hello")  

# This will work because the tuple is unpacked into separate arguments
greet(*("Hello", "Alice"))  # prints "Hello, Alice!"

# This will also work because the dictionary is unpacked into separate keyword arguments
greet(**{"greeting": "Hello", "name": "Alice"})  # prints "Hello, Alice!"

Best Practices

  • When using **kwargs, keep in mind that the arguments will be passed to the function as a dictionary. This means that you will not be able to modify the arguments within the function.
  • Consider using **kwargs only when you need to accept a variable number of keyword arguments that do not have a specific meaning or purpose.