class a(object):
data={'a':'aaa','b':'bbb','c':'ccc'}
def pop(self, key, *args):
return self.data.pop(key, *args)#what is this mean.
b=a()
print b.pop('a',{'b':'bbb'})
print b.data
self.data.pop(key, *args) ←—— why is there a second argument?
The
popmethod of dicts (likeself.data, i.e.{'a':'aaa','b':'bbb','c':'ccc'}, here) takes two arguments — see the docsThe second argument,
default, is whatpopreturns if the first argument,key, is absent.(If you call
popwith just one argument,key, it raises an exception if that key’s absent).In your example,
print b.pop('a',{'b':'bbb'}), this is irrelevant because'a'is a key inb.data. But if you repeat that line…:you’ll see it makes a difference: the first
popremoves the'a'key, so in the secondpopthedefaultargument is actually returned (since'a'is now absent fromb.data).