Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python's help syntax

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use Python's help grammar". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Python's help grammar".

I. Notes

Make sure you use the correct style for modules, functions, methods, and inline comments

One-line comments begin with #

# this is a comment print ("Hello, World!")

Single quotes ('')

#! / usr/bin/python3''' this is a multiline comment, with three single quotes''print ("Hello, World!")

Double quotation marks (")

#! / usr/bin/python3 "" this is a multiline comment, with three single quotes "" print ("Hello, World!") II. DIR

Syntax: dir ([object])

Description:

Returns a list of variables, methods, and defined types in the current scope when no parameters are passed.

> dir () ['_ _ builtins__','_ _ doc__','_ _ loader__','_ _ name__','_ _ package__','_ spec__'] > a = 10 # definition variable a > dir () # there is an extra a ['_ builtins__','_ doc__','_ loader__','_ name__','_ package__','_ spec__' 'a']

When the parameter object is a module, the list of properties and methods of the module is returned.

> import math > > math > dir (math) ['_ _ doc__','_ loader__','_ _ name__','_ package__','_ _ spec__', 'acos',' acosh', 'asin',' asinh', 'atan',' atan2', 'atanh',' ceil', 'copysign',' cos', 'cosh',' degrees', 'eBay,' erf', 'erfc' 'exp', 'expm1',' fabs', 'factorial',' floor', 'fmod',' frexp', 'fsum',' gamma', 'gcd',' hypot', 'inf',' isclose', 'isfinite',' isinf', 'isnan',' ldexp', 'lgamma',' log', 'log10',' log1p', 'log2',' modf', 'nan',' pi', 'pow' 'radians', 'sin',' sinh', 'sqrt',' tan', 'tanh',' trunc']

Returns a list of properties and methods of the class and its subclasses when the parameter object is a class.

> class A: name = 'class' > a = A () > dir (a) # name is an attribute of class A. The other is the property and method of object inherited by default ['_ class__','_ _ delattr__','_ _ dict__','_ dir__','_ _ doc__','_ _ eq__','_ _ format__','_ ge__','_ getattribute__','_ gt__','_ _ hash__','_ _ init__' '_ _ le__',' _ _ lt__','_ _ module__','_ _ ne__','_ _ new__','_ _ reduce__','_ _ reduce_ex__','_ _ repr__','_ _ setattr__','_ _ sizeof__','_ str__','_ subclasshook__','_ _ weakref__', 'name']

When the object defines the _ _ dir__ method, the result of the _ _ dir__ method is returned

> class B: def _ dir__ (self): return ['name','age'] > b = B () > > dir (b) # call _ _ dir__ method [' age', 'name'] III, _ _ doc__

Writing documents in programs is a feature of LISP, and Python has also borrowed from it. Each function is an object, and each function object has a property of _ _ doc__. In the function statement, if the first expression is a string, the function's _ _ doc__ is the string, otherwise _ _ doc__ is None.

> def testfun (): "this function do nothing, just demostrate the use of the doc string."pass > testfun.__doc__'\ nthis function do nothing, just demostrate the use of the doc string.\ n'> # pass statement is an empty statement and does nothing, just like {} in C language, by displaying _ _ doc__ We can check the help information for some internal functions > ".join. _ _ doc__'S.join (iterable)-> str\ n\ nReturn a string which is the concatenation of the strings in the\ niterable. The separator between elements is S.' > > IV. Help

Syntax: help ([object])

Description:

In the interpreter interface, when the function is called without passing parameters, the built-in help system will be activated and entered. When entering the names of modules, classes, functions, etc., inside the help system, the instructions for their use will be displayed, enter quit to exit the built-in help system, and return to the interactive interface.

> help () # without parameters Welcome to Python 3.5 s help utilityloaded if this is your first time using Python, you should definitely check outthe tutorial on the Internet at Enter the name of any module, keyword, or topic to get help on writingPython programs and using Python modules. To quit this help utility andreturn 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 comeswith an one-line summary of what it does; to list the modules whose nameor summary contain a given string such as "spam", type "modules spam". # enter the built-in help system > become help > help > str # str help information Help on class strin module builtins: class str (object) | str (object='')-> str | str (bytes_or_buffer [, encoding [, help]])-> str | | Create a new string object from the given object. If encoding or | errors is specified, then the object must expose a data buffer | that will be decoded using the given encoding and error handler. | Otherwise, returns the result of object.__str__ () (if defined) | or repr (object). | encoding defaults to sys.getdefaultencoding (). | errors defaults to 'strict'. | | Methods defined here: | | _ _ add__ (self, value, /) | Return self+value.. | Help > 1 # module name, class name, function name No Python documentation found for '1'.Use help () to get the interactive help utility.Use help (str) for help on the str class. Help > quit # exit the built-in help system You are now leaving help and returning to the Python interpreter.If you want to ask for help on a particular object directly from theinterpreter, you can type "help (object)". Executing "help ('string')" has the same effect as typing a particular string at the help > prompt. # quit the built-in help system and return to the interactive interface help > become >

In the interpreter interface, when a parameter is passed in to call a function, it will find whether the parameter is a module name, class name, function name, and if so, instructions for its use will be displayed.

> > help (str) Help on class strin module builtins: class str (object) | str (object='')-> str | str (bytes_or_buffer [, encoding [, errors]])-> str | | Create a new string object from the given object. If encoding or | errors is specified, then the object must expose a data buffer | that will be decoded using the given encoding and error handler. | Otherwise, returns the result of object.__str__ () (if defined) | or repr (object). | encoding defaults to sys.getdefaultencoding (). | errors defaults to 'strict'. | | Methods defined here: | | _ _ add__ (self, value) | /) | Return self+value. | * * Thank you for your reading The above is the content of "how to use Python's help syntax". After the study of this article, I believe you have a deeper understanding of how to use Python's help syntax, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report