Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python help()

#1
Python help()

Python’s built-in help() function launches Python’s help system. Without an argument, help() starts an interactive session. With a string argument, help(name) looks up the name and prints a help page to the shell. With a non-string argument, help(object) prints a help page on object.

>>> help(list) # Prints the documentation of list
>>> help(dict) # Prints the documentation of dict
>>> help(int) # Prints the documentation of int
>>> help('help') # Prints the documentation of help()
>>> help() # Opens an interactive "help" session


Syntax help()


The help() object has the following syntax:

Syntax: 
help() # Opens an interactive help session to explore multiple objects
help(string) # Looks up the name and prints a help page to the shell
help(object) # Prints a help page on object

Arguments object Optional. If given, prints a help page on the object. If it’s a string, it looks up the mapping name –> object and prints the help page on the object. If not given, opens an interactive help session.
Return Value None Returns None.

Return Value help()


The help(object) function returns None.

Example help() without Argument


Without an argument, help() starts an interactive session.

>>> help() Welcome to Python 3.7's help utility! If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.7/tutorial/. Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit". To get a list of available modules, keywords, symbols, or topics, type "modules", "keywords", "symbols", or "topics". Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam". help> string
Help on module string: NAME string - A collection of string constants. DESCRIPTION Public module variables:

In this example, you created the interactive help session and passed the name string (which is an object) within the session. Python then prints the associated documentation. You can close the interactive help session by hitting CTRL + C or type quit, followed by hitting enter.

Example help() with String Argument


With a string argument, help(name) looks up the name and prints a help page to the shell.

>>> help('help') Help on module help: NAME help DESCRIPTION help.py: Implement the Idle help menu. Contents are subject to revision at any time, without notice.
...

You may ask: where does Python look up the objects associated to each string value?

The answer is simple: with the help of namespaces, it looks up names of modules, functions, methods, variables in the Python paths. Given the name, it can then get the associated objects from the memory mapping—each name points to an object in memory.

You can read more in our full tutorials:

Example help() with Object Argument


With a non-string argument, help(object) prints a help page on object.

>>> help(print) Help on built-in function print in module builtins: print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream.

The passed function object associated to the print name also works as an input for the help() function. In this case, no look up is needed—but the documentation of the passed object is printed right away.

Python help() Function Docstring


Python’s help(object) function returns the docstring associated to the object. The docstring is a string that follows immediately after the definition of a module, function, method, or object.

Here’s an example of defining a function add() with a docstring:

def add(a, b): '''Calculates a+b and returns the sum.''' return a+b 

And here’s how you can access this in your code via the help(add) function call:

>>> help(add)
Help on function add in module __main__: add(a, b) Calculates a+b and returns the sum.

This way, you can write your own documentation and provide other coders access to your helpful insights when writing code snippets.

Python help() Function on Custom Class


You can also call the help() function on classes. Python will then show you the available methods and their docstrings. Here’s an example:

class Car: '''Representing cool cars''' def __init__(self, brand, color): '''Creates a new car with a brand and color''' self.brand = brand self.color = color def speed(): '''Returns the speed of the car given brand''' if self.brand == 'porsche': return 150 return 100 

Each method and the class itself has a docstring. The output when calling the help() function on the class Car is the following:

>>> help(Car)
Help on class Car in module __main__: class Car(builtins.object) | Car(brand, color) | | Representing cool cars | | Methods defined here: | | __init__(self, brand, color) | Creates a new car with a brand and color | | speed() | Returns the speed of the car given brand | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined)

But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners


Summary


Python’s built-in help() function launches Python’s help system.

  • Without an argument, help() starts an interactive session—see the following code snippet.
  • With a string argument, help(name) looks up the name and prints a help page to the shell.
  • With a non-string argument, help(object) prints a help page on object.
>>> help() Welcome to Python 3.7's help utility! If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.7/tutorial/. Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit". To get a list of available modules, keywords, symbols, or topics, type "modules", "keywords", "symbols", or "topics". Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam". help> string
Help on module string: NAME string - A collection of string constants. DESCRIPTION Public module variables:
...

I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:

Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?


Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post Python help() first appeared on Finxter.



https://www.sickgaming.net/blog/2021/01/14/python-help/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Compare Rpm Versions in Python? xSicKxBot 0 6 03-01-2021, 07:10 PM
Last Post: xSicKxBot
  [Tut] How to Open a PDF File in Python? xSicKxBot 0 16 02-22-2021, 11:39 AM
Last Post: xSicKxBot
  [Tut] Python Get Milliseconds xSicKxBot 0 33 02-19-2021, 08:28 AM
Last Post: xSicKxBot
  [Tut] Python vars() Function xSicKxBot 0 25 02-18-2021, 04:10 PM
Last Post: xSicKxBot
  [Tut] Python object() Function xSicKxBot 0 32 02-16-2021, 08:16 PM
Last Post: xSicKxBot
  [Tut] Python str() Function xSicKxBot 0 28 02-14-2021, 06:05 AM
Last Post: xSicKxBot
  [Tut] Python issubclass() xSicKxBot 0 31 02-12-2021, 10:40 AM
Last Post: xSicKxBot
  [Tut] Python int() Function xSicKxBot 0 40 02-04-2021, 08:21 PM
Last Post: xSicKxBot
  [Tut] How to Send UDP Multicast in Python? xSicKxBot 0 35 02-03-2021, 01:31 PM
Last Post: xSicKxBot
  [Tut] How To Cut A String In Python? xSicKxBot 0 32 02-01-2021, 04:09 PM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
Unreal Engine March 2021 Giveaway
Last Post: xSicKxBot
Today 05:59 PM
» Replies: 0
» Views: 1
Mobile - Warhammer 40k: Lost Crusade cod...
Last Post: xSicKxBot
Today 05:58 PM
» Replies: 0
» Views: 0
Microsoft - Microsoft cloud: A cloud bui...
Last Post: xSicKxBot
Today 05:57 PM
» Replies: 0
» Views: 0
Microsoft - New nation-state cyberattack...
Last Post: xSicKxBot
Today 05:57 PM
» Replies: 0
» Views: 0
News - Eight-Year-Old Becomes Youngest E...
Last Post: xSicKxBot
Today 05:57 PM
» Replies: 0
» Views: 1
News - Blizzard Dev Warns Against Diablo...
Last Post: xSicKxBot
Today 05:56 PM
» Replies: 0
» Views: 2
[Tut] Python Dictionary Get Value – A Si...
Last Post: xSicKxBot
Today 09:59 AM
» Replies: 0
» Views: 3
(Indie Deal) FREE Hope Lake, Larry's Bun...
Last Post: xSicKxBot
Today 09:59 AM
» Replies: 0
» Views: 4
News - Destiny 2 Hotfix 3.1.0.2
Last Post: xSicKxBot
Today 08:00 AM
» Replies: 0
» Views: 5
Xbox Wire - Fire up the Time Machine: Th...
Last Post: xSicKxBot
Today 07:59 AM
» Replies: 0
» Views: 5

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020