In Python, what does dict.pop (a, b) mean?

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) & larr; ------ why is there a second argument?

+58
python dictionary
Jan 02 '09 at 6:40
source share
4 answers

The pop dicts method (for example, self.data , i.e. {'a':'aaa','b':'bbb','c':'ccc'} , here) takes two arguments - see the docs

The second argument to default is what pop returns if the first argument to key missing. (If you call pop just one argument, key , it throws an exception if that key is missing).

In your example, print b.pop('a',{'b':'bbb'}) does not matter, since 'a' is the key in b.data . But if you repeat this line ...:

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

you will see that it matters: the first pop removes the key 'a' , so in the second pop the default argument is actually returned (since 'a' now missing in b.data ).

+79
Jan 02 '09 at 6:49
source share
โ€” -

There are so many questions. I see at least two, maybe three:

  • What does pop (a, b) do? / Why is there a second argument?
  • What is *args ?



The first question is trivially answered in the Python standard library :

pop (key [, by default])

If the key is in the dictionary, delete it and return its value, otherwise return the default value. If the default value is not specified and the key is not in the dictionary, a KeyError occurs.




The second question is addressed in the Python Language Reference :

If the form "* identifier" is present, it is initialized by receiving a tuple any redundant positional parameters, by default for an empty tuple. If the form "** identifier" is present, it is initialized to a new dictionary; getting extra keyword arguments; the default is for a new empty dictionary.

In other words, the pop function takes at least two arguments. The first two are named self and key ; and the rest is inserted into a tuple called args .

What happens on the next line when *args is passed in a call to self.data.pop is the reverse of this - the *args tuple expands to positional parameters that are passed together. This is explained in the Python Language Reference :

If the syntax expression * appears in a function call, the expression must evaluate the sequence. Elements from this sequence are processed 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 to self.data.pop() .

It gives you flexibility; data now a dict , so self.data.pop() takes one or two parameters; but if you changed data to a type that took 19 parameters to call self.data.pop() , you would not have to change the class a at all. You still have to change any code called a.pop() to pass the necessary 19 parameters.

+18
Jan 02 '09 at 6:50
source share
 def func(*args): pass 

When you define a function this way, *args will be an array of arguments passed to the function. This allows your function to work without knowing in advance how many arguments will be passed to it.

You can also use keyword arguments using **kwargs :

 def func2(**kwargs): pass 

See: Arbitrary Argument Lists




In your case, you have defined a class that acts like a dictionary. The dict.pop method dict.pop defined as pop(key[, default]) .

Your method does not use the default parameter. But by defining your method with *args and passing *args to dict.pop() , you allow the caller to use the default parameter.

In other words, you should use a method of the pop class, for example dict.pop :

 my_a = a() value1 = my_a.pop('key1') # throw an exception if key1 isn't in the dict value2 = my_a.pop('key2', None) # return None if key2 isn't in the dict 
+5
Jan 02 '09 at 6:46
source share
 >>> def func(a, *args, **kwargs): ... print 'a %s, args %s, kwargs %s' % (a, args, kwargs) ... >>> func('one', 'two', 'three', four='four', five='five') a one, args ('two', 'three'), kwargs {'four': 'four', 'five': 'five'} 



 >>> def anotherfunct(beta, *args): ... print 'beta %s, args %s' % (beta, args) ... >>> def func(a, *args, **kwargs): ... anotherfunct(a, *args) ... >>> func('one', 'two', 'three', four='four', five='five') beta one, args ('two', 'three') >>> 
+5
Jan 02 '09 at 9:56
source share



All Articles