3

Just recently I have been getting very a bizarre issue, which I have found no resolve to. For example, if I run the following code in the interpreter

>>> def test():
...     'docstring'
...     ...
... 
>>> print(test.__doc__)

I get 'docstring,' no surprise there. Now if I run this in a *.py file, I get None.

I have tried multiple files with the same results; but why does the interpreter return the docstring and not any files? Any help would most certainly be appreciated.

Note: I have tried triple quotes, double and single, and any other variation I can muster, with no luck.

Solved: Not sure how exactly, but the command I was using was 'broken' in that shell instance. Working fine now

poke
  • 369,085
  • 72
  • 557
  • 602
Cubli
  • 39
  • 5
  • @AvinashRaj I have, this is just a quick synopsis of my problem. – Cubli Jan 22 '16 at 05:23
  • If you print `__file__` do you get the value you expect? – Ignacio Vazquez-Abrams Jan 22 '16 at 05:24
  • @IgnacioVazquez-Abrams Yes. – Cubli Jan 22 '16 at 05:25
  • 1
    How are you running the file? please include your command line. – shx2 Jan 22 '16 at 06:41
  • 1
    It works fine for me (as a *.py file) in both Python 2.7.10 and 3.5.1 -- it also works when I change the print statement to help(test). What does the '...' on your third line standing in for? (I.e. give us more code.) – cdlane Jan 22 '16 at 07:11
  • @cdlane '...' is ellipsis. – Cubli Jan 22 '16 at 07:20
  • @Cubli With actual ellipsis you would get `SyntaxError`. – skyking Jan 22 '16 at 07:22
  • I'm not able to reproduce your problem. Tried `python t.py`, `python3 t.py` and `import t` from interactive shell (both python v2 and v3). Please provide an exact description of what you've done: actual source code, how it's run (command line) and which python version you're using. – skyking Jan 22 '16 at 07:26
  • @shx2 It apparently was an issue with the command. Thanks. – Cubli Jan 22 '16 at 07:26
  • @skyking Why exactly? I have used Ellipsis as a placeholder in lots of code, with no such exception. Also, it was an issue with the command, but I restarted my command line so I don't know the cause in the command. – Cubli Jan 22 '16 at 07:27
  • Please show **exactly** the contents of the file you are trying to run. And no, using a ellipsis there isn’t a syntax error (but you should really get used to using `pass` in these cases instead). – poke Jan 22 '16 at 07:36
  • @poke At least my python(2) reports `SyntaxError` there. – skyking Jan 22 '16 at 07:50
  • @Cubli Because one should produce a minimal, complete, verifiable example (http://stackoverflow.com/help/mcve) in order to get an explaination of why your code doesn't work. Exactly, because often it turns out that the asker forgot some important detail when posting a slightly different example (your question seem to fall into this category). Here for example if we try what you've written it doesn't work on python2 and on python3 you don't get the behavior you describe. It's important here to note that you're using python3 since it's illegal in python2. – skyking Jan 22 '16 at 07:54
  • @skyking That’s because `...` is an alias to `Ellipsis` in Python 3 (but not Python 2). – poke Jan 22 '16 at 07:56

1 Answers1

6

The docstrings are stripped if you run the interpreter with the -OO switch:

-OO Discard docstrings in addition to the -O optimizations.

This can be also controlled by the PYTHONOPTIMIZE environment variable:

PYTHONOPTIMIZE

If this is set to a non-empty string it is equivalent to specifying the -O option. If set to an integer, it is equivalent to specifying -O multiple times.

Thus you could get this effect if you had a Python wrapper that specified -OO, or PYTHONOPTIMIZE was set to say 2.


However, this setting should also apply to the interactive interpreter:

% PYTHONOPTIMIZE=2 python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo():
...     'bar'
... 
>>> foo.__doc__
>>> 

You can test the optimization settings at runtime via sys.flags:

% PYTHONOPTIMIZE=2 python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.flags.optimize
2

If this number is greater than or equal to 2, then that is why your docstrings are gone.