Why does the .get () chain dictionary in python return a tuple if it is not a tuple by default?

Python 2.6.6, when I call .get on .get results, the result is a tuple. It makes no sense to me. Example:

box = {}.get('test1',{}).get('test2','hrmm'), print type(box) 

displays

 <type 'tuple'> 

it makes no sense to me. it is clear that by default in the second get there is a simple line. so what gives? Thanks for any insight.

+6
source share
2 answers

You have a trailing comma at the end of the line, so you get the result {}.get('test1',{}).get('test2','hrmm') in a singleton tuple.

Here is an example of how this works with a simple literal:

 >>> box = 1, >>> box (1,) >>> type(box) <type 'tuple'> 
+12
source

Your drawer assignment has a trailing comma

+2
source

All Articles