Funny behaviour with __future__ and doctest between 2.6 and 3.1

Mattsteel mattsteel at hotmail.it
Fri Jan 29 16:32:58 CET 2010


Hello all.
I'm using Python 2.6.4 and Python 3.1.1.
My wish is to code in a 3.1-compliant way using 2.6, so I'm importing
the __future__ module.
I've found a funny thing comparing the two folliwing snippets that
differ for one line only, that is the position of __future__ import
(before or after the doc string).

Well, I understand the subtle difference but still I wander what
really happen behind the scenes.
Comments are welcome.

---------------------------------------
    #!/usr/bin/env python
    '''
    >>> concat('hello','world')
    'hello world'
    '''
    from __future__  import unicode_literals
    def concat( first, second ):
        return first + ' ' + second
    if __name__ == "__main__":
        import doctest
        doctest.testmod()
---------------------------------------
    #!/usr/bin/env python
    from __future__  import unicode_literals
    '''
    >>> concat('hello','world')
    'hello world'
    '''
    def concat( first, second ):
        return first + ' ' + second
    if __name__ == "__main__":
        import doctest
        doctest.testmod()
---------------------------------------


The first way shows the following failure:

---------------------------------------
  Failed example:
      concat('hello','world')
  Expected:
      'hello world'
  Got:
      u'hello world'

---------------------------------------

Regards.

Matt.



More information about the Python-list mailing list