[New-bugs-announce] [issue37033] Dictionary defaults .get(), .pop(), .setdefault()

Vykouk report at bugs.python.org
Fri May 24 05:38:42 EDT 2019


New submission from Vykouk <vykouk at incoengineering.cz>:

The methods .get(), .pop(), .setdefault() evaluate defaults even though the key already exists in the dictionary:

# -*- coding: utf-8 -*-

def deflt(x):
    print('\nKey', x, 'is not in the dictionary')
    return 'Default'

dicti = {1:'one', 2:'two', 3:'three'}

key = 2
print('\ndicti \t\t', dicti)
print('\t\t key =',key)

print('get \t\t',     dicti.get(key, deflt(key)))
print('setdefault \t',dicti.setdefault(key, deflt(key)))
print('dicti \t\t',   dicti)
print('pop \t\t',     dicti.pop(key, deflt(key)))
print('dicti \t\t',   dicti)
print('setdefault \t',dicti.setdefault(key, deflt(key)))
print('dicti \t\t',   dicti)

'''
Printed outputs:

Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 7.1.1 -- An enhanced Interactive Python.

runfile('C:/Temp/Smazat/Dictionary_Defaults.py', wdir='C:/Temp/Smazat')

dicti            {1: 'one', 2: 'two', 3: 'three'}
                 key = 2

Key 2 is not in the dictionary                 #  ???        
get              two

Key 2 is not in the dictionary                 #  ???
setdefault       two
dicti            {1: 'one', 2: 'two', 3: 'three'}

Key 2 is not in the dictionary                 #  ???
pop              two
dicti            {1: 'one', 3: 'three'}

Key 2 is not in the dictionary
setdefault       Default
dicti            {1: 'one', 3: 'three', 2: 'Default'}

'''

----------
files: Dictionary_Defaults.py
messages: 343362
nosy: vykouk
priority: normal
severity: normal
status: open
title: Dictionary defaults .get(), .pop(), .setdefault()
type: behavior
versions: Python 3.7
Added file: https://bugs.python.org/file48352/Dictionary_Defaults.py

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37033>
_______________________________________


More information about the New-bugs-announce mailing list