The pop method of dicts (like self.data, i.e. {'a':'aaa','b':'bbb','c':'ccc'}, here) takes two arguments -- see the docs
The second argument, default, is what pop returns if the first argument, key, is absent.
(If you call pop with 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 in b.data. But if you repeat that line...:
b=a()
print b.pop('a',{'b':'bbb'})
print b.pop('a',{'b':'bbb'})
print b.data
you'll see it makes a difference: the first pop removes the 'a' key, so in the second pop the default argument is actually returned (since 'a' is now absent from b.data).
The pop method of dicts (like self.data, i.e. {'a':'aaa','b':'bbb','c':'ccc'}, here) takes two arguments -- see the docs
The second argument, default, is what pop returns if the first argument, key, is absent.
(If you call pop with 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 in b.data. But if you repeat that line...:
b=a()
print b.pop('a',{'b':'bbb'})
print b.pop('a',{'b':'bbb'})
print b.data
you'll see it makes a difference: the first pop removes the 'a' key, so in the second pop the default argument is actually returned (since 'a' is now absent from b.data).
So many questions here. I see at least two, maybe three:
- What does pop(a,b) do?/Why are there a second argument?
- What is
*argsbeing used for?
The first question is trivially answered in the Python Standard Library reference:
pop(key[, default])
If key is in the dictionary, remove it and return its value, else return default. If default is not given and key is not in the dictionary, a KeyError is raised.
The second question is covered in the Python Language Reference:
If the form “*identifier” is present, it is initialized to a tuple receiving any excess positional parameters, defaulting to the empty tuple. If the form “**identifier” is present, it is initialized to a new dictionary receiving any excess keyword arguments, defaulting to a new empty dictionary.
In other words, the pop function takes at least two arguments. The first two get assigned the names self and key; and the rest are stuffed into a tuple called args.
What's happening on the next line when *args is passed along in the call to self.data.pop is the inverse of this - the tuple *args is expanded to of positional parameters which get passed along. This is explained in the Python Language Reference:
If the syntax *expression appears in the function call, expression must evaluate to a sequence. Elements from this sequence are treated as if they were additional positional arguments
In short, a.pop() wants to be flexible and accept any number of positional parameters, so that it can pass this unknown number of positional parameters on to self.data.pop().
This gives you flexibility; data happens to be a dict right now, and so self.data.pop() takes either one or two parameters; but if you changed data to be a type which took 19 parameters for a call to self.data.pop() you wouldn't have to change class a at all. You'd still have to change any code that called a.pop() to pass the required 19 parameters though.
Videos
dict.pop(v) works like list.remove(v) and dict.popitem() is similar to list.pop() and you can do del data[x] on bought but the __getitem__ in the dict evaluates an object instead a index and you can't access the items by the index but you can use the dict.keys() to translate an index to a value so you can override this functionality to work as a list with something like this:
class EDict(dict):
def pop(self, index=-1):
return dict.pop(self, tuple(self.keys())[index])
data = EDict({
'1': 'a', '2': 'b',
'3': 'c', '4': 'd',
})
if __name__ == '__main__':
print(data)
data.pop()
print(data)
data.pop(1)
print(data)
(Amiga500 was probably referring to this post)
I don't have a definitive answer, but I have two thoughts on the subject.
Three methods of Python dictionaries have optional default parameter:
dict.get(),dict.setdefault()and, yes,dict.pop(). All of them essentially return value, associated with the key, so adding default to all of those is a consistent thing to do.Note that
dict.popitem()doesn't accept the key, and consistently doesn't need defult.Another thing is basic convenience. Lists are sequences, and it's hard to imagine a case where you would need a default for
list.pop(). If you try to pop an item from outside of list "bounds", you've got a bug, and papering over possible failure withlist.pop(index, None)wouldn't do much good. On the other hand, it's quite common for dictionary to not contain a value for a specific key, so looking it up might very well be a normal situation.