3 ways to get help from Python __python

Source: Internet
Author: User
Tags acos asin cos function prototype hypot mathematical functions pow sin

We can easily get help through the Python interpreter. If you want to know more about an object, you can call Help (object). There are also useful ways in which dir (object) displays most of the associated property names for that object, and Object._ doc _ displays its corresponding document string. Here's a description of it. 1. Help ()

The Help function is a built-in function of Python.
Function prototype: Help ([Object]).
can help us understand more about the object.
If no argument is given, the interactive Help system starts on the interpreter console.

>>> help () Welcome to Python 2.7!

This are the online Help utility. The If is your the should definitely check out the tutorial on the Internet at Http://docs.pytho

n.org/2.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 the interpreter, just type "quit".  To get a list of available modules, keywords, or topics, type "modules", "keywords", or "topics". Each module also comes with a one-line summary of what it does;

To list the modules whose summaries contain a given word such as "spam", type "modules spam".  help> int # Because of the space problem, only part of the content is shown here, as in the class int in module __builtin__: Class int (object) |  Int (x=0)-> int or Long |  

int (x, base=10)-> int or Long | ... help> 

If The argument is a string, then the string is looked up as the name of a module, function, class, method, keyword, or do Cumentation topic, and a help page are printed on the console. If The argument is any other kind of object, a help page on the object is generated.

>>> Help (ABS)  # View ABS function Help on
built-in function abs in module __BUILTIN__:

abs (...)
    ABS (number)-> number return the

    absolute value of the argument.

>>> Help (Math) # View the Math module, where only part of the content help is shown in
built-in module math:

NAME
    Math

FILE
    ( Built-in)

DESCRIPTION This
    module is always available.  It provides access to the
    mathematical functions defined by the C standard.

Functions
    ACOs (...)
        ACOs (x) return the

        arc cosine (measured in radians) of X.

.....

>>> 
2, dir ()

The Dir function is a built-in function of Python.
Function prototype: dir ([Object])
Can help us get most of the related properties of the object.
Without arguments, return to the list of names in the current local scope.

>>> dir ()  # no parameters
[' __builtins__ ', ' __doc__ ', ' __name__ ', ' __package__ ']
>>> 
> >> Import Math  # introduces a package and a variable, again dir ()
>>> a=3
>>> 
>>> dir ()
[' __ Builtins__ ', ' __doc__ ', ' __name__ ', ' __package__ ', ' a ', ' math ']
>>> 

With an argument, attempt to return a list of valid attributes for that object.

>>> Import Math
>>> dir (math)  # Math module as Parameters
[' __doc__ ', ' __name__ ', ' __package__ ', ' ACOs ', ' acosh ', ' asin ', ' Asinh ', ' atan ', ' atan2 ', ' Atanh ', ' ceil ', ' copysign ', ' cos ', ' cosh ', ' degrees ', ' e ', ' Erf ', ' ERFC ', ' exp ', ' expm1 ', ' fabs ', ' factorial ', ' floor ', ' fmod ', ' frexp ', ' fsum ', ' gamma ', ' hypot ', ' isinf ', ' isNaN ', ' ldexp ', ' LG Amma ', ' Log ', ' log10 ', ' log1p ', ' modf ', ' pi ', ' pow ', ' radians ', ' sin ', ' sinh ', ' sqrt ', ' tan ', ' tanh ', ' trunc ']
>> ;> 

The default dir () mechanism behaves differently with different types of objects, as it attempts to produce the most Releva NT, rather than complete, information:
if the object is a module object, the list contains the names of the module ' s attributes.

>>> Import Math
>>> dir (math)  # Math module as Parameters
[' __doc__ ', ' __name__ ', ' __package__ ', ' ACOs ', ' acosh ', ' asin ', ' Asinh ', ' atan ', ' atan2 ', ' Atanh ', ' ceil ', ' copysign ', ' cos ', ' cosh ', ' degrees ', ' e ', ' Erf ', ' ERFC ', ' exp ', ' expm1 ', ' fabs ', ' factorial ', ' floor ', ' fmod ', ' frexp ', ' fsum ', ' gamma ', ' hypot ', ' isinf ', ' isNaN ', ' ldexp ', ' LG Amma ', ' Log ', ' log10 ', ' log1p ', ' modf ', ' pi ', ' pow ', ' radians ', ' sin ', ' sinh ', ' sqrt ', ' tan ', ' tanh ', ' trunc ']
>> ;> 

if the "object is" a type or class object, the list contains the names of its attributes, and recursively of the attribute S of its bases.

>>> dir (float) # type [' __abs__ ', ' __add__ ', ' __class__ ', ' __coerce__ ', ' __delattr__ ', ' __div__ ', ' __divmod__ ', ' _ _doc__ ', ' __eq__ ', ' __float__ ', ' __floordiv__ ', ' __format__ ', ' __ge__ ', ' __getattribute__ ', ' __getformat__ ', ' __ Getnewargs__ ', ' __gt__ ', ' __hash__ ', ' __init__ ', ' __int__ ', ' __le__ ', ' __long__ ', ' __lt__ ', ' __mod__ ', ' __mul__ ', ' __ne_ _ ', ' __neg__ ', ' __new__ ', ' __nonzero__ ', ' __pos__ ', ' __pow__ ', ' __radd__ ', ' __rdiv__ ', ' __rdivmod__ ', ' __reduce__ ', ' __ Reduce_ex__ ', ' __repr__ ', ' __rfloordiv__ ', ' __rmod__ ', ' __rmul__ ', ' __rpow__ ', ' __rsub__ ', ' __rtruediv__ ', ' __setattr_ _ ', ' __setformat__ ', ' __sizeof__ ', ' __str__ ', ' __sub__ ', ' __subclasshook__ ', ' __truediv__ ', ' __trunc__ ', ' As_integer_ Ratio ', ' conjugate ', ' fromhex ', ' hex ', ' imag ', ' is_integer ', ' real ' >>> dir (3.4) [' __abs__ ', ' __add__ ', ' __clas S__ ', ' __coerce__ ', ' __delattr__ ', ' __div__ ', ' __divmod__ ', ' __doc__ ', ' __eq__ ', ' __float__ ', ' __floordiv__ ', ' __format __ ', ' __ge__ ', ' __getattribute__ ', ' __getformat__ ',' __getnewargs__ ', ' __gt__ ', ' __hash__ ', ' __init__ ', ' __int__ ', ' __le__ ', ' __long__ ', ' __lt__ ', ' __mod__ ', ' __mul__ ', ' __ ne__ ', ' __neg__ ', ' __new__ ', ' __nonzero__ ', ' __pos__ ', ' __pow__ ', ' __radd__ ', ' __rdiv__ ', ' __rdivmod__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __rfloordiv__ ', ' __rmod__ ', ' __rmul__ ', ' __rpow__ ', ' __rsub__ ', ' __rtruediv__ ', ' __ Setattr__ ', ' __setformat__ ', ' __sizeof__ ', ' __str__ ', ' __sub__ ', ' __subclasshook__ ', ' __truediv__ ', ' __trunc__ ', ' As_
    Integer_ratio ', ' conjugate ', ' fromhex ', ' hex ', ' imag ', ' is_integer ', ' real '] >>> >>> class a:x=3  
 Y=4 >>> class B (A): z=5 >>> dir (b) # class [' __doc__ ', ' __module__ ', ' x ', ' y ', ' z '] >>>

Otherwise, the list contains the object ' s attributes ' names, the names of its class ' s attributes, and recursively of the Attributes of its class ' s base classes. 3, _ Doc_

There is a wonderful feature in Python, a document string, also known as docstrings.
It can be used to add descriptive text to our modules, classes, functions, etc. to make the program easy to read and understand, and, more importantly, to export these descriptive text messages through the standard methods Python brings.
The standard method mentioned above is _ doc _. Before and after each two underline.
Note : When a call to Doc is not a function, method, module, etc., the document string for the constructor of the type that this object is dependent on is displayed.

>>> Import Math >>> math.__doc__ # modules ' This module is always available. It provides access to the\nmathematical functions defined by the C standard. ' >>> abs.__doc__ # built-in functions ' abs (Numbe
    R)-> Number\n\nreturn the absolute value of the argument. ' >>> def ADDXY (x,y): ' ' The sum of x and Y ' return x+y >>> addxy.__doc__ # Custom function ' The sum of x and y ' >>> a=[1,2,4] >>> a.count.__doc_   _ # method ' L.count (value)-> integer--return number of occurrences of value ' >>> b=3 >>> b.__doc__ # Concrete object ' int (x=0)-> int or long\nint (x, base=10)-> int or long\n\nconvert a number or string to an integer, or ret  Urn 0 if no arguments\nare given. If x is floating point, the conversion truncates towards zero.\nif x is outside the integer range, the function returns a Long instead.\n\nif x is isn't a number or if base is given, then x must being a string Or\nunicode object representing an inte GER literal in the Given base.  The\nliteral can be preceded by ' + ' or '-' and is surrounded by whitespace.\nthe base defaults to 10.  Valid bases are 0 and 2-36. Base 0 means To\ninterpret the base from the string as an integer literal.\n>>> int (' 0b100 ', base=0) \n4 ">> 
 >

In fact, we can use a certain means to view these document strings, such as using Pycharm, in the corresponding modules, functions, methods, and so on the mouse "right click"->go to->declaration. For example: View a document string for the built-in function abs

Let's take another example of a specific object, for example, the doc of the concrete integer object B above shows the document string of the type int it is subordinate to:

Reference documents:
1. Python Help documentation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.