Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python doc strings print \n instead of a new line (Python 2.7.5 on a Mac)

So here is what I am typing

>>> list = [1,2,3]
>>> list.__doc__
"list() -> new empty list\nlist(iterable) -> new list initialized from iterable's items"

The problem being that it literally prints "\n" instead of printing a new line, making more complicated doc strings almost impossible to read.

The same thing happens with all of the other built-in classes I have tried.

The version information I get from python when I open it is: Python 2.7.5 (default, Jul 16 2013, 14:23:29) [GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin

like image 714
sicklybeans Avatar asked Oct 19 '25 19:10

sicklybeans


1 Answers

That is because the list.__doc__ is a string.

When you just type list.__doc__ you will get the contents like this :

>>> list.__doc__
"list() -> new empty list\nlist(iterable) -> new list initialized from iterable's items"

However if you type print list.__doc__

>>> print list.__doc__
list() -> new empty list
list(iterable) -> new list initialized from iterable's items

So the print function properly formats the string to include the linebreak.

Hope that helps :)

like image 152
woofmeow Avatar answered Oct 22 '25 09:10

woofmeow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!