-2

Seems the dict.pop() can distinguish between user-specified "default" and real default(omitted).
For example(real result):

my_dict.pop('non-exist-key', None)
my_dict.pop('non-exist-key')
#Traceback (most recent call last):
#  File "<stdin>", line 1, in <module>
#KeyError: 'non-exist-key'

I expect dict.pop has one default value for the "default" parameter otherwise I expect result:

#Throw exception in caller rather than in callee(my_dict.pop)
my_dict.pop('non-exist-key')
#TypeError: my_dict.pop() missing 1 required positional argument: 'default'
illiterate
  • 289
  • 2
  • 12
  • 2
    There isn't one, which is why you get the error – roganjosh Nov 29 '19 at 09:07
  • 2
    The docs you link to say exactly that, " If default is not given and key is not in the dictionary, a KeyError is raised." – Sayse Nov 29 '19 at 09:07
  • @roganjosh I believe there is one default value, see my update. – illiterate Nov 29 '19 at 09:18
  • Why do you believe that? – Sayse Nov 29 '19 at 09:19
  • @sayse see my update. if it no default it should throw one TypeError in caller. – illiterate Nov 29 '19 at 09:21
  • Why should it? You've linked to the documents that very clearly state the expected behaviours – Sayse Nov 29 '19 at 09:21
  • @roganjosh - [Possible duplicate](https://stackoverflow.com/q/17326067/1324033) then, although this is a very poorly explained question. – Sayse Nov 29 '19 at 09:33
  • @sayse "Why should it?" In other word I excepted `TypeError: my_dict.pop() missing 1 required positional argument: 'default'` except my_dict.pop() has one default value for its "default" parameter – illiterate Nov 29 '19 at 09:36
  • @sayse I'm sorry, "excepted" is typo of "expected". – illiterate Nov 29 '19 at 09:43
  • @roganjosh Why you delete almost all your answer and comments in this question? I think them is valuable for me. – illiterate Nov 30 '19 at 17:47
  • 1
    I realised that I didn't understand it myself. FWIW it prompted me to ask [my own question](https://stackoverflow.com/questions/59103311/how-does-cpython-determine-whether-a-user-supplied-an-optional-argument). I deleted because I can't be sure I'm giving you correct info – roganjosh Nov 30 '19 at 20:58
  • @roganjosh I still don't know real implement details in python, but I realized such things can implement in python code by [check number of parameters](https://stackoverflow.com/a/33073692/9377221). – illiterate Dec 01 '19 at 20:18

1 Answers1

1

The pop() method removes and returns an element from a dictionary having the given key.

syntax is dictionary.pop(key[, default])

The pop() method returns:

  1. If key is found - removed/popped element from the dictionary
  2. If key is not found - value specified as the second argument (default)
  3. If key is not found and default argument is not specified - KeyError exception is raised

    example_dict = {"a": 1, "b": 2}

We initialised the dictionary here.

example_dict.pop("c", None)

This statement returns None as we have specified it in second argument.

example_dict.pop("c")

This will raise keyerror.

def pop(self, *args):
    """
    Remove and return the value for a key.
    """
    have_default = len(args) == 2
    try:
        v = self[args[0]]
    except KeyError:
        if have_default:
            return args[1]
        raise
    else:
        del self[args[0]]
        return v

This is pure python implementation of pop function.

  • Why it don't reaise one TypeError of "required positional argument"? – illiterate Nov 30 '19 at 17:56
  • Because that is an optional argument, the function call works even if argument is not given. Please accept the answer if it clarifies your doubt. – Amog Chandrashekar Dec 02 '19 at 06:54
  • It don't clarifies me. I still don't know real implement details in python, but I realized such things can implement in python code by [check number of parameters](https://stackoverflow.com/a/33073692/9377221) rather than just provide one regular default parameter in the `def` statement. – illiterate Dec 02 '19 at 12:06
  • Check the python implementation that i have added to the answer. – Amog Chandrashekar Dec 03 '19 at 07:51