Python uses dictionary get() method TypeError: get() takes no keyword arguments

Solution

dictget("key", 0)Do not add to the method default=. Deleting this wording does not affect the use of logic, but adding it will cause an error.

d = {
    
    
    'key': 2,
}
print(d.get("key", 0))

Problem resolution

If you use the following code, an error will be reportedTypeError: get() takes no keyword arguments

d = {
    
    
    'key': 2,
}
print(d.get("key", default=0))

The reason is that the bottom layer of Python is written in C, and the name of this parameter cannot be parsed when calling the underlying C language. The current bottom layer design of Python cannot solve this problem, so just pass in the parameter directly, don’t add it default=, the syntax is fine .

Guess you like

Origin blog.csdn.net/weixin_35757704/article/details/114778657