New submission from Vykouk <vyk...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue37033>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to