title: Force Python function to return type 'tuple'
body: <p>I've run into an interesting problem I can't seem to solve.  I have a pretty complicated system that calls a formatting tool, which is defined as a class for each format we support.  The class name is determined dynamically and the value is formatted based on our client's documentation for an API POST.</p>
  
  <p>The problem I've run into is that some values require a single key/value pair <code>(key, value)</code> whereas some require multiple pairs, which I put into a list of tuples <code>[(key1, value1), (key2, value2)]</code>.</p>
  
  <p>What I need to do is get the key(s)/value(s) and create a tuple of tuples and pass it along for delivery.  I cannot use a dictionary as order may be of significance later.</p>
  
  <p>The overall structure of this code is quite vast so I'll try to break it down into small pieces for readability.</p>
  
  <p>calling_function:</p>
  
  <pre><code>def map_lead(self, lead):
      mapto_data = tuple()
      for offer_field in self.offerfield_set.all():
          field_name = offer_field.field.name
          if field_name not in lead.data:
              raise LeadMissingField(lead, field_name)
  
          formatted_list = format_value(offer_field.mapto, lead.data[field_name])
  
          if type(formatted_list).__name__ == 'list':
              for item in formatted_list:
                  mapto_data += (item,)
  
          elif type(formatted_list).__name__ == 'tuple':
              mapto_data += (formatted_list)
          return mapto_data
  </code></pre>
  
  <p>example_format_type1:</p>
  
  <pre><code>@staticmethod
  def do_format(key, value):
      area_code, exchange, number = PhoneFormat.format_phone(value)
          return [
              (PhoneFormat.AREA_CODE_MAPTO, area_code),
              (PhoneFormat.PHONE_EXCHANGE_MAPTO, exchange),
              (PhoneFormat.VANTAGE_MEDIA_HOME_PHONE_NUMBER_MAPTO, number)
          ]
  </code></pre>
  
  <p>example_format_type2:</p>
  
  <pre><code>@staticmethod
  def do_format(key, value):
      if len(value) &gt; 3:
          value = value[:3] + '-' + value[3:]
          if len(value) &gt; 7:
              value = value[:7] + '-' + value[7:]
      return key, value
  </code></pre>
  
  <p>I've tried to explicitly define the return value of the <code>example_format_type2</code> to be a tuple:</p>
  
  <pre><code>@staticmethod
  def do_format(key, value):
      if len(value) &gt; 3:
          value = value[:3] + '-' + value[3:]
          if len(value) &gt; 7:
              value = value[:7] + '-' + value[7:]
      formatted_value = tuple()
      formatted_value += (key, value)
      return formatted_value
  </code></pre>
  
  <p>But it seems that regardless of what I do it gets interpreted as a list in <code>calling_function</code>.</p>
  
  <p>So, I always get <code>type(formatted_list).__name__ == 'list'</code>.  So, if it is a tuple I'm returning the <code>for</code> loop goes through each item in the tuple and adds it as a single value in the <code>mapto_data</code> tuple.</p>
  
  <p>Is there a way to force Python to return the value from <code>example_format_type2</code> such that it will be interpreted in <code>calling_function</code> as a tuple?</p>
  
  <p>EDIT1:</p>
  
  <p>It turns out the problem was in <code>map_lead</code> where I was adding to the <code>mapto_data</code> tuple.  I missed the trailing comma there.</p>