Python help函数
程序员文章站
2022-05-11 08:03:43
python 3.x版本虽然比2.x少了一些内置函数,但是 python 内置 函数没有60个,也有40个,那么多内置函数你记得过来吗?为了方便使用,python 提供了help函数专门用来提供查看函数或模块用途的详细说明; 一.help函数简介 语法如下: help([object]) 参数: o ......
python 3.x版本虽然比2.x少了一些内置函数,但是函数没有60个,也有40个,那么多内置函数你记得过来吗?为了方便使用,python 提供了help函数专门用来提供查看函数或模块用途的详细说明;
一.help函数简介
语法如下:
help([object])
参数:
object – 对象/函数名/模块;
返回值 – 返回对象的帮助信息;
二.help函数使用
1.查看内置函数的帮助信息
print(help(type)) # 内置函数type - 获取数据变量类型
输出结果:
help on class type in module builtins: class type(object) | type(object_or_name, bases, dict) | type(object) -> the object's type | type(name, bases, dict) -> a new type | | methods defined here: | | __call__(self, /, *args, **kwargs) | call self as a function. | | __delattr__(self, name, /) | implement delattr(self, name). | | __dir__(...) | __dir__() -> list | specialized __dir__ implementation for types | | __getattribute__(self, name, /) | return getattr(self, name). | | __init__(self, /, *args, **kwargs) | initialize self. see help(type(self)) for accurate signature. | | __instancecheck__(...) | __instancecheck__() -> bool | check if an object is an instance | | __new__(*args, **kwargs) | create and return a new object. see help(type) for accurate signature. | | __prepare__(...) | __prepare__() -> dict | used to create the namespace for the class statement | | __repr__(self, /) | return repr(self). | | __setattr__(self, name, value, /) | implement setattr(self, name, value). | | __sizeof__(...) | __sizeof__() -> int | return memory consumption of the type object | | __subclasscheck__(...) | __subclasscheck__() -> bool | check if a class is a subclass | | __subclasses__(...) | __subclasses__() -> list of immediate subclasses | | mro(...) | mro() -> list | return a type's method resolution order | | ---------------------------------------------------------------------- | data descriptors defined here: | | __abstractmethods__ | | __dict__ | | __text_signature__ | | ---------------------------------------------------------------------- | data and other attributes defined here: | | __base__ = <class 'object'> | the most base type | | __bases__ = (<class 'object'>,) | | __basicsize__ = 864 | | __dictoffset__ = 264 | | __flags__ = -2146675712 | | __itemsize__ = 40 | | __mro__ = (<class 'type'>, <class 'object'>) | | __weakrefoffset__ = 368 none process finished with exit code 0
2.查看数据类型的帮助信息
# !usr/bin/env python # -*- coding:utf-8 _*- """ @author:何以解忧 @blog(个人博客地址): shuopython.com @wechat official account(微信公众号):猿说编程 @github:www.github.com @file:python_help.py @time:2020/03/20 09:35 @motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! """ print(help(str)) # 内置数据变量类型 - 字符
输出结果:
help on class str in 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. | | __contains__(self, key, /) | return key in self. | | __eq__(self, value, /) | return self==value. | | __format__(...) | s.__format__(format_spec) -> str | | return a formatted version of s as described by format_spec. | | __ge__(self, value, /) | return self>=value. | | __getattribute__(self, name, /) | return getattr(self, name). | | __getitem__(self, key, /) | return self[key]. | | __getnewargs__(...) | | __gt__(self, value, /) | return self>value. | | __hash__(self, /) | return hash(self). | | __iter__(self, /) | implement iter(self). | | __le__(self, value, /) | return self<=value. | | __len__(self, /) | return len(self). | | __lt__(self, value, /) | return self<value. | | __mod__(self, value, /) | return self%value. | | __mul__(self, value, /) | return self*value.n | | __ne__(self, value, /) | return self!=value. | | __new__(*args, **kwargs) from builtins.type | create and return a new object. see help(type) for accurate signature. | | __repr__(self, /) | return repr(self). | | __rmod__(self, value, /) | return value%self. | | __rmul__(self, value, /) | return self*value. | | __sizeof__(...) | s.__sizeof__() -> size of s in memory, in bytes | | __str__(self, /) | return str(self). | | capitalize(...) | s.capitalize() -> str | | return a capitalized version of s, i.e. make the first character | have upper case and the rest lower case. | | casefold(...) | s.casefold() -> str | | return a version of s suitable for caseless comparisons. | | center(...) | s.center(width[, fillchar]) -> str | | return s centered in a string of length width. padding is | done using the specified fill character (default is a space) | | count(...) | s.count(sub[, start[, end]]) -> int | | return the number of non-overlapping occurrences of substring sub in | string s[start:end]. optional arguments start and end are | interpreted as in slice notation. | | encode(...) | s.encode(encoding='utf-8', errors='strict') -> bytes | | encode s using the codec registered for encoding. default encoding | is 'utf-8'. errors may be given to set a different error | handling scheme. default is 'strict' meaning that encoding errors raise | a unicodeencodeerror. other possible values are 'ignore', 'replace' and | 'xmlcharrefreplace' as well as any other name registered with | codecs.register_error that can handle unicodeencodeerrors. | | endswith(...) | s.endswith(suffix[, start[, end]]) -> bool | | return true if s ends with the specified suffix, false otherwise. | with optional start, test s beginning at that position. | with optional end, stop comparing s at that position. | suffix can also be a tuple of strings to try. | | expandtabs(...) | s.expandtabs(tabsize=8) -> str | | return a copy of s where all tab characters are expanded using spaces. | if tabsize is not given, a tab size of 8 characters is assumed. | | find(...) | s.find(sub[, start[, end]]) -> int | | return the lowest index in s where substring sub is found, | such that sub is contained within s[start:end]. optional | arguments start and end are interpreted as in slice notation. | | return -1 on failure. | | format(...) | s.format(*args, **kwargs) -> str | | return a formatted version of s, using substitutions from args and kwargs. | the substitutions are identified by braces ('{' and '}'). | | format_map(...) | s.format_map(mapping) -> str | | return a formatted version of s, using substitutions from mapping. | the substitutions are identified by braces ('{' and '}'). | | index(...) | s.index(sub[, start[, end]]) -> int | | return the lowest index in s where substring sub is found, | such that sub is contained within s[start:end]. optional | arguments start and end are interpreted as in slice notation. | | raises valueerror when the substring is not found. | | isalnum(...) | s.isalnum() -> bool | | return true if all characters in s are alphanumeric | and there is at least one character in s, false otherwise. | | isalpha(...) | s.isalpha() -> bool | | return true if all characters in s are alphabetic | and there is at least one character in s, false otherwise. | | isdecimal(...) | s.isdecimal() -> bool | | return true if there are only decimal characters in s, | false otherwise. | | isdigit(...) | s.isdigit() -> bool | | return true if all characters in s are digits | and there is at least one character in s, false otherwise. | | isidentifier(...) | s.isidentifier() -> bool | | return true if s is a valid identifier according | to the language definition. | | use keyword.iskeyword() to test for reserved identifiers | such as "def" and "class". | | islower(...) | s.islower() -> bool | | return true if all cased characters in s are lowercase and there is | at least one cased character in s, false otherwise. | | isnumeric(...) | s.isnumeric() -> bool | | return true if there are only numeric characters in s, | false otherwise. | | isprintable(...) | s.isprintable() -> bool | | return true if all characters in s are considered | printable in repr() or s is empty, false otherwise. | | isspace(...) | s.isspace() -> bool | | return true if all characters in s are whitespace | and there is at least one character in s, false otherwise. | | istitle(...) | s.istitle() -> bool | | return true if s is a titlecased string and there is at least one | character in s, i.e. upper- and titlecase characters may only | follow uncased characters and lowercase characters only cased ones. | return false otherwise. | | isupper(...) | s.isupper() -> bool | | return true if all cased characters in s are uppercase and there is | at least one cased character in s, false otherwise. | | join(...) | s.join(iterable) -> str | | return a string which is the concatenation of the strings in the | iterable. the separator between elements is s. | | ljust(...) | s.ljust(width[, fillchar]) -> str | | return s left-justified in a unicode string of length width. padding is | done using the specified fill character (default is a space). | | lower(...) | s.lower() -> str | | return a copy of the string s converted to lowercase. | | lstrip(...) | s.lstrip([chars]) -> str | | return a copy of the string s with leading whitespace removed. | if chars is given and not none, remove characters in chars instead. | | partition(...) | s.partition(sep) -> (head, sep, tail) | | search for the separator sep in s, and return the part before it, | the separator itself, and the part after it. if the separator is not | found, return s and two empty strings. | | replace(...) | s.replace(old, new[, count]) -> str | | return a copy of s with all occurrences of substring | old replaced by new. if the optional argument count is | given, only the first count occurrences are replaced. | | rfind(...) | s.rfind(sub[, start[, end]]) -> int | | return the highest index in s where substring sub is found, | such that sub is contained within s[start:end]. optional | arguments start and end are interpreted as in slice notation. | | return -1 on failure. | | rindex(...) | s.rindex(sub[, start[, end]]) -> int | | return the highest index in s where substring sub is found, | such that sub is contained within s[start:end]. optional | arguments start and end are interpreted as in slice notation. | | raises valueerror when the substring is not found. | | rjust(...) | s.rjust(width[, fillchar]) -> str | | return s right-justified in a string of length width. padding is | done using the specified fill character (default is a space). | | rpartition(...) | s.rpartition(sep) -> (head, sep, tail) | | search for the separator sep in s, starting at the end of s, and return | the part before it, the separator itself, and the part after it. if the | separator is not found, return two empty strings and s. | | rsplit(...) | s.rsplit(sep=none, maxsplit=-1) -> list of strings | | return a list of the words in s, using sep as the | delimiter string, starting at the end of the string and | working to the front. if maxsplit is given, at most maxsplit | splits are done. if sep is not specified, any whitespace string | is a separator. | | rstrip(...) | s.rstrip([chars]) -> str | | return a copy of the string s with trailing whitespace removed. | if chars is given and not none, remove characters in chars instead. | | split(...) | s.split(sep=none, maxsplit=-1) -> list of strings | | return a list of the words in s, using sep as the | delimiter string. if maxsplit is given, at most maxsplit | splits are done. if sep is not specified or is none, any | whitespace string is a separator and empty strings are | removed from the result. | | splitlines(...) | s.splitlines([keepends]) -> list of strings | | return a list of the lines in s, breaking at line boundaries. | line breaks are not included in the resulting list unless keepends | is given and true. | | startswith(...) | s.startswith(prefix[, start[, end]]) -> bool | | return true if s starts with the specified prefix, false otherwise. | with optional start, test s beginning at that position. | with optional end, stop comparing s at that position. | prefix can also be a tuple of strings to try. | | strip(...) | s.strip([chars]) -> str | | return a copy of the string s with leading and trailing | whitespace removed. | if chars is given and not none, remove characters in chars instead. | | swapcase(...) | s.swapcase() -> str | | return a copy of s with uppercase characters converted to lowercase | and vice versa. | | title(...) | s.title() -> str | | return a titlecased version of s, i.e. words start with title case | characters, all remaining cased characters have lower case. | | translate(...) | s.translate(table) -> str | | return a copy of the string s in which each character has been mapped | through the given translation table. the table must implement | lookup/indexing via __getitem__, for instance a dictionary or list, | mapping unicode ordinals to unicode ordinals, strings, or none. if | this operation raises lookuperror, the character is left untouched. | characters mapped to none are deleted. | | upper(...) | s.upper() -> str | | return a copy of s converted to uppercase. | | zfill(...) | s.zfill(width) -> str | | pad a numeric string s with zeros on the left, to fill a field | of the specified width. the string s is never truncated. | | ---------------------------------------------------------------------- | static methods defined here: | | maketrans(x, y=none, z=none, /) | return a translation table usable for str.translate(). | | if there is only one argument, it must be a dictionary mapping unicode | ordinals (integers) or characters to unicode ordinals, strings or none. | character keys will be then converted to ordinals. | if there are two arguments, they must be strings of equal length, and | in the resulting dictionary, each character in x will be mapped to the | character at the same position in y. if there is a third argument, it | must be a string, whose characters will be mapped to none in the result. none -10 process finished with exit code 0
猜你喜欢:
转载请注明:猿说python »
技术交流、商务合作请直接联系博主
扫码或搜索:猿说编程
猿说编程
微信公众号 扫一扫关注
上一篇: php获取mysql版本的几种方法小结_php技巧
下一篇: IOS文件上传