Discussion:
[Tutor] Basic question about docstrings
Emile van Sebille
2015-07-29 19:57:46 UTC
Permalink
Hi
"""
Some text ...
:param x: Some value
:returns: Something useful
"""
What is the most basic way of showing those docstrings at the Python prompt?
help(get_value)
Emile

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Danny Yoo
2015-07-29 20:00:13 UTC
Permalink
Hi
"""
Some text ...
:param x: Some value
:returns: Something useful
"""
What is the most basic way of showing those docstrings at the Python
prompt?

Try:

help(get_value)

At the Python prompt. This uses the built in help facility:

https://docs.python.org/2/library/functions.html#help
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
David Aldrich
2015-07-30 08:24:27 UTC
Permalink
"""
Some text ...
:param x: Some value
:returns: Something useful
"""
What is the most basic way of showing those docstrings at the Python
prompt?
help(get_value)
https://docs.python.org/2/library/functions.html#help
I understand that 'help' works with modules that I have imported. But if I've just written a script called main.py (which contains get_value()) I don't think I can 'import' that. So how would I see the docstrings in main.py?

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Joel Goldstick
2015-07-30 16:15:31 UTC
Permalink
On Thu, Jul 30, 2015 at 4:24 AM, David Aldrich
Post by David Aldrich
"""
Some text ...
:param x: Some value
:returns: Something useful
"""
What is the most basic way of showing those docstrings at the Python
prompt?
help(get_value)
https://docs.python.org/2/library/functions.html#help
I understand that 'help' works with modules that I have imported. But if I've just written a script called main.py (which contains get_value()) I don't think I can 'import' that. So how would I see the docstrings in main.py?
Try it! you'll find it works fine
--
Joel Goldstick
http://joelgoldstick.com
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano
2015-07-30 16:26:11 UTC
Permalink
Post by David Aldrich
I understand that 'help' works with modules that I have imported. But
if I've just written a script called main.py (which contains
get_value()) I don't think I can 'import' that. So how would I see the
docstrings in main.py?
import main
help(main)


Of course, you have to write main.py so it is intended to be imported.
If you write main.py as a regular script, then importing it the first
time will run the script, which you don't want.

The trick is to put the script functionality in a function, say:

# File script.py

def do_this():
"""This is a helper function"""
...

def main():
"""Main function."""
print("Processing...")
do_this()
do_that()
do_something_else()
print("Done!")

if __name__ == "__main__":
# Run the main script.
main()


By using that pattern, the file "script.py" can be imported at the
interactive interpreter:

import script
help(script.main)
help(script.do_this) # etc.

but of you run it directly, outside the interactive intepreter, the
main() function will run and it will behave as a script as expected.

The reason this works is that when you run a script, Python sets the
magic variable __name__ to "__main__". But when you import it, the
variable is set to the actual name of the file (minus the .py
extension).
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano
2015-07-30 17:09:37 UTC
Permalink
<snip>
Post by Steven D'Aprano
import main
help(get_field)
File "<stdin>", line 1, in <module>
NameError: name 'get_field' is not defined
Naturally. Name resolution works the same for help() as for anything
else.

If you say:

import math

then you need to refer to math.sin and math.cos, not sin or cos on their
own. The same applies to your main.get_field, whether you are using
help() or not.
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Danny Yoo
2015-07-31 01:49:44 UTC
Permalink
<snip>
Post by Steven D'Aprano
import main
help(get_field)
File "<stdin>", line 1, in <module>
NameError: name 'get_field' is not defined
help(main) works ok but is rather verbose.
Try:

import main
help(main.get_field)
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Loading...