简体   繁体   中英

Passing a dictionary to a function with unpacking argument

This below snippet of code gives me this error TypeError: pop() argument after ** must be a mapping, not tuple .

class a():
    data={'a':'aaa','b':'bbb','c':'ccc'}
    def pop(self, key, **args):
        return self.data.pop(key, **args)

b=a()
print(b.pop('a',{'b':'bbb'}))

But when I replace double ** with single * , this works fine. As per my understanding , if we are passing a dictionary , we should have double ** . In this case the second argument what's being passed is dictionary {'b':'bbb'} . Then how is it throwing error in first case but not in second case?

class a():
    data={'a':'aaa','b':'bbb','c':'ccc'}
    def pop(self, key, *args):
        return self.data.pop(key, *args)

b=a()
print(b.pop('a',{'b':'bbb'})

If you want a dictionary to be used as keyword arguments, you have to use the ** in the call as well:

print(b.pop('a',**{'b':'bbb'}))

But I don't think that's really what you wanted anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM