Last updated: Apr 8, 2024
Reading time·3 min
The Python "TypeError: X takes no keyword arguments" occurs when we pass a keyword argument to a function that only takes positional arguments.
To solve the error, pass positional arguments to the function.
def get_name(first, last): return first + ' ' + last # ⛔️ Calling the function with keyword arguments print(get_name(first='bobby', last='hadz')) # ✅ Calling the function with positional arguments print(get_name('bobby', 'hadz'))
The first call to the get_name
function uses keyword arguments and the second
call uses positional arguments.
Keyword arguments take the form of argument_name=value
and positional
arguments are passed directly by value.
THe get_name
function in the example can be called both ways without any
issues.
Instead, you should pass positional arguments to these methods.
Here are 2 examples of how the error occurs with built-in methods and how to solve it.
The "TypeError: str.replace() takes no keyword arguments" occurs when we pass
keyword arguments to the str.replace()
method.
To solve the error, pass only positional arguments to replace()
, e.g.
my_str.replace('old', 'new')
.
Here is an example of how the error occurs.
my_str = 'apple, apple, banana' # ⛔️ TypeError: str.replace() takes no keyword arguments result = my_str.replace(old='apple', new='kiwi')
The error was caused because we passed keyword arguments to the replace()
method.
The replace()
method takes only positional arguments.
my_str = 'apple, apple, banana' result = my_str.replace('apple', 'kiwi') print(result) # 👉️ kiwi, kiwi, banana
The str.replace() method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.
The method takes the following parameters:
Name | Description |
---|---|
old | The substring we want to replace in the string |
new | The replacement for each occurrence of old |
count | Only the first count occurrences are replaced (optional) |
Note that the method doesn't change the original string. Strings are immutable in Python.
If you need to use a regular expression when replacing characters in a string, use the re.sub method.
import re my_str = '1apple, 2apple, 3banana' result = re.sub(r'[0-9]', '_', my_str) print(result) # 👉️ _apple, _apple, _banana
The re.sub
method returns a new string that is obtained by replacing the
occurrences of the pattern with the provided replacement.
If the pattern isn't found, the string is returned as is.
The "TypeError: dict.get() takes no keyword arguments" occurs when we pass
keyword arguments to the dict.get()
method.
To solve the error, only pass positional arguments to dict.get()
, e.g.
my_dict.get('my_key', 'default')
.
Here is an example of how the error occurs.
my_dict = {'name': 'Bobby Hadz', 'age': 30} # ⛔️ TypeError: dict.get() takes no keyword arguments result = my_dict.get('name', default='')
The error was caused because we passed a keyword argument to the dict.get()
method.
The dict.get()
method takes only positional arguments.
my_dict = {'name': 'Bobby Hadz', 'age': 30} print(my_dict.get('name', '')) # 👉️ "Alice" print(my_dict.get('another', 'default')) # 👉️ 'default'
The dict.get() method returns the value for the given key if the key is in the dictionary, otherwise a default value is returned.
The method takes the following 2 parameters:
Name | Description |
---|---|
key | The key for which to return the value |
default | The default value to be returned if the provided key is not present in the dictionary (optional) |
default
parameter is not provided, it defaults to None
, so the get()
method never raises a KeyError
.Both of the arguments the dict.get()
method takes are positional.
You can't pass a keyword argument to the method.
If the provided key exists in the dict
object, its value is returned,
otherwise, the get()
method returns the supplied default
value or None
if
one wasn't provided when calling dict.get()
.
You can learn more about the related topics by checking out the following tutorials: