16

Python 2.6.6 when I call .get on the results of a .get the result is a tuple. This is making no sense to me. Example:

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

prints out

<type 'tuple'>

this makes no sense to me. clearly the default in the second get is a simple string. so what gives? thanks for any insight.

Blade McCool
  • 163
  • 1
  • 4

2 Answers2

38

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

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

>>> box = 1,
>>> box
(1,)
>>> type(box)
<type 'tuple'>
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
  • 1
    +1. Python doesn't have many traps for beginners, but the role of commas in the syntax for tuple literals is one of them. Usually it bites the other way around, though - people expecting `(foo)` to be a tuple containing `foo` when in fact it is just `foo`. This question demonstrates an amusing subversion of the typical mistake. – Mark Amery Nov 28 '12 at 23:59
  • Thank you kindly. I feel very silly right now. Ha Ha. Yes it makes sense of course now the comma would create a sequence type object of some kind. I completely was blind to see the trailing comma throughout many modifications and trials. – Blade McCool Nov 29 '12 at 01:05
6

There is a trailing comma at your box assignment

Jesse the Game
  • 2,600
  • 16
  • 21