Funny behaviour with __future__ and doctest between 2.6 and 3.1

Peter Otten __peter__ at web.de
Fri Jan 29 11:30:30 EST 2010


Mattsteel wrote:

> Sadly (for me), you're right... then the only way to use doctest to
> work both in 2.6 and 3.1 (without modifications between them) is
> something like this:
> 
> #!/usr/bin/env python
> '''
>>>> str(concat('hello','world'))
> 'hello world'
> '''
> from __future__  import unicode_literals
> def concat( first, second ):
>     return first + ' ' + second
> if __name__ == "__main__":
>     import doctest
>     doctest.testmod()
> 
> Is there any way to avoid using str(...) to protect the string?
> M.

I think you can work around the problem. The following should pass in Python 
2.6 and 3.1:

'''
>>> concat('hello','world') == 'hello world'
True
'''
from __future__  import unicode_literals

def concat( first, second ):
    return first + ' ' + second

if __name__ == "__main__":
    import doctest
    doctest.testmod()

Peter



More information about the Python-list mailing list