Skip to main content

How to Resolve Python TypeError: X takes no keyword arguments

The TypeError: X takes no keyword arguments is a common Python error indicating that you've tried to call a function or method using keyword argument syntax (argument_name=value) when that specific function/method was designed to only accept arguments based on their position. This often happens with certain built-in methods.

This guide explains why this error occurs and provides clear solutions, focusing on common cases like str.replace() and dict.get().

Understanding the Error: Positional vs. Keyword Arguments

Python functions/methods can accept arguments in two main ways:

  • Positional Arguments: Passed based on their order. The function matches the first value passed to the first parameter, the second value to the second parameter, and so on.
    def example(a, b):          # Defines positional parameters a, b
    print(f"a={a}, b={b}")
    example(10, 20) # 10 goes to a, 20 goes to b
  • Keyword Arguments: Passed using the parameter_name=value syntax. The order doesn't matter as Python matches them by name.
    def example_kw(a=0, b=0):   # Defines parameters (can have defaults)
    print(f"a={a}, b={b}")
    example_kw(b=20, a=10) # Order doesn't matter

Most user-defined Python functions accept both positional and keyword arguments (unless defined with special syntax like / or * to restrict them). However, the TypeError: ... takes no keyword arguments arises when you encounter a function or method specifically designed to accept only positional arguments.

The Cause: Calling Positional-Only Functions/Methods with Keywords

The error occurs because you provided an argument in the name=value format to a function or method that doesn't recognize argument names. It only understands arguments based on their position in the call.

General Solution: Use Positional Arguments

The universal solution to this error is to remove the argument_name= part and ensure you are passing the arguments in the correct order expected by the function or method. You'll need to know the expected positional parameters for the specific function/method you are calling.

Example 1: TypeError: str.replace() takes no keyword arguments

Error Scenario

You try to use named arguments like old= and new= when calling the string's replace() method.

my_string = "apple pie apple sauce"

try:
# ⛔️ TypeError: str.replace() takes no keyword arguments
# Using keyword arguments 'old' and 'new'
result = my_string.replace(old='apple', new='cherry')
print(result)
except TypeError as e:
print(e)

Solution: Use Positional Arguments for replace()

The str.replace() method expects its arguments positionally: replace(old_substring, new_substring, count=optional_limit). Provide the values directly in that order.

my_string = "apple pie apple sauce"

# ✅ Pass arguments positionally: (old, new, [count])
# Replace ALL occurrences
result_all = my_string.replace('apple', 'cherry')
print(f"Replaced all: '{result_all}'")
# Output: Replaced all: 'cherry pie cherry sauce'

# Replace only the FIRST occurrence using the optional 'count' parameter
result_first = my_string.replace('apple', 'cherry', 1) # 1 is the count
print(f"Replaced first: '{result_first}'")
# Output: Replaced first: 'cherry pie apple sauce'

Consult the documentation (or use help(str.replace)) to know the correct positional order for such methods.

Example 2: TypeError: dict.get() takes no keyword arguments

Error Scenario

You attempt to use a keyword argument like default= when calling the dictionary's get() method.

my_dictionary = {'name': 'Alice', 'city': 'London'}

try:
# ⛔️ TypeError: dict.get() takes no keyword arguments
# Using keyword argument 'default'
country = my_dictionary.get('country', default='N/A')
print(country)
except TypeError as e:
print(e)

Solution: Use Positional Arguments for get()

The dict.get() method expects positional arguments: get(key, default=optional_default_value). Provide the key and the optional default value directly.

my_dictionary = {'name': 'Alice', 'city': 'London'}

# Key exists
name = my_dictionary.get('name', 'Unknown') # 'Unknown' is the default
print(f"Name: {name}") # Output: Name: Alice

# Key does not exist, default is returned
country = my_dictionary.get('country', 'N/A') # 'N/A' is the default
print(f"Country: {country}") # Output: Country: N/A

# Key does not exist, no default provided (returns None)
state = my_dictionary.get('state')
print(f"State: {state}") # Output: State: None

Why Do Some Functions Not Accept Keyword Arguments?

This behavior is often seen in:

  • Built-in Functions/Methods Implemented in C: Many core Python functions and methods for built-in types (like str, list, dict) are implemented in C for performance. Historically, exposing parameter names for keyword argument usage from C was more complex or not always done.
  • Design Choice: In some cases, a function might be intentionally designed to only accept positional arguments for clarity or specific ordering requirements, although this is less common for newly written Python code unless specific markers (/) are used in the function definition.

Conclusion

The TypeError: X takes no keyword arguments clearly indicates you've used the name=value syntax when calling a function or method that only accepts arguments based on their position.

The solution is straightforward:

  1. Identify the function/method causing the error (e.g., str.replace, dict.get).
  2. Consult its documentation (or use help()) to understand its expected positional parameters and their order.
  3. Modify your function call to pass the arguments positionally, without the name= part, ensuring they are in the correct sequence.

By understanding the distinction between positional and keyword arguments and recognizing which functions require positional-only input, you can easily fix this common TypeError.