python常用内置函数(二)

忘是亡心i 2021-12-17 01:23 432阅读 0赞

python常用内置函数(二)

  • help()
  • id()
  • dir()

help()

  1. 功能:用于查看函数或模块用途的详细说明。
  2. 参数:一个对象

实例

  1. print(help('keyword'))
  2. print(help(list))
  3. print(help(list.append))

输出

  1. Help on module keyword:
  2. NAME
  3. keyword - Keywords (from "graminit.c")
  4. DESCRIPTION
  5. This file is automatically generated; please don't muck it up!
  6. To update the symbols in this file, 'cd' to the top directory of
  7. the python source tree after building the interpreter and run:
  8. ./python Lib/keyword.py
  9. FUNCTIONS
  10. iskeyword = __contains__(...) method of builtins.frozenset instance
  11. x.__contains__(y) <==> y in x.
  12. DATA
  13. __all__ = ['iskeyword', 'kwlist']
  14. kwlist = ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'aw...
  15. FILE
  16. d:\python\pythoninstall\lib\keyword.py
  17. None
  18. Help on class list in module builtins:
  19. class list(object)
  20. | list(iterable=(), /)
  21. |
  22. | Built-in mutable sequence.
  23. |
  24. | If no argument is given, the constructor creates a new empty list.
  25. | The argument must be an iterable if specified.
  26. |
  27. | Methods defined here:
  28. |
  29. | __add__(self, value, /)
  30. | Return self+value.
  31. |
  32. | __contains__(self, key, /)
  33. | Return key in self.
  34. |
  35. | __delitem__(self, key, /)
  36. | Delete self[key].
  37. |
  38. | __eq__(self, value, /)
  39. | Return self==value.
  40. |
  41. | __ge__(self, value, /)
  42. | Return self>=value.
  43. |
  44. | __getattribute__(self, name, /)
  45. | Return getattr(self, name).
  46. |
  47. | __getitem__(...)
  48. | x.__getitem__(y) <==> x[y]
  49. |
  50. | __gt__(self, value, /)
  51. | Return self>value.
  52. |
  53. | __iadd__(self, value, /)
  54. | Implement self+=value.
  55. |
  56. | __imul__(self, value, /)
  57. | Implement self*=value.
  58. |
  59. | __init__(self, /, *args, **kwargs)
  60. | Initialize self. See help(type(self)) for accurate signature.
  61. |
  62. | __iter__(self, /)
  63. | Implement iter(self).
  64. |
  65. | __le__(self, value, /)
  66. | Return self<=value.
  67. |
  68. | __len__(self, /)
  69. | Return len(self).
  70. |
  71. | __lt__(self, value, /)
  72. | Return self<value.
  73. |
  74. | __mul__(self, value, /)
  75. | Return self*value.
  76. |
  77. | __ne__(self, value, /)
  78. | Return self!=value.
  79. |
  80. | __repr__(self, /)
  81. | Return repr(self).
  82. |
  83. | __reversed__(self, /)
  84. | Return a reverse iterator over the list.
  85. |
  86. | __rmul__(self, value, /)
  87. | Return value*self.
  88. |
  89. | __setitem__(self, key, value, /)
  90. | Set self[key] to value.
  91. |
  92. | __sizeof__(self, /)
  93. | Return the size of the list in memory, in bytes.
  94. |
  95. | append(self, object, /)
  96. | Append object to the end of the list.
  97. |
  98. | clear(self, /)
  99. | Remove all items from list.
  100. |
  101. | copy(self, /)
  102. | Return a shallow copy of the list.
  103. |
  104. | count(self, value, /)
  105. | Return number of occurrences of value.
  106. |
  107. | extend(self, iterable, /)
  108. | Extend list by appending elements from the iterable.
  109. |
  110. | index(self, value, start=0, stop=9223372036854775807, /)
  111. | Return first index of value.
  112. |
  113. | Raises ValueError if the value is not present.
  114. |
  115. | insert(self, index, object, /)
  116. | Insert object before index.
  117. |
  118. | pop(self, index=-1, /)
  119. | Remove and return item at index (default last).
  120. |
  121. | Raises IndexError if list is empty or index is out of range.
  122. |
  123. | remove(self, value, /)
  124. | Remove first occurrence of value.
  125. |
  126. | Raises ValueError if the value is not present.
  127. |
  128. | reverse(self, /)
  129. | Reverse *IN PLACE*.
  130. |
  131. | sort(self, /, *, key=None, reverse=False)
  132. | Stable sort *IN PLACE*.
  133. |
  134. | ----------------------------------------------------------------------
  135. | Static methods defined here:
  136. |
  137. | __new__(*args, **kwargs) from builtins.type
  138. | Create and return a new object. See help(type) for accurate signature.
  139. |
  140. | ----------------------------------------------------------------------
  141. | Data and other attributes defined here:
  142. |
  143. | __hash__ = None
  144. None
  145. Help on method_descriptor:
  146. append(self, object, /)
  147. Append object to the end of the list.
  148. None

id()

  1. 功能:用于获取对象的内存地址。
  2. 参数:一个对象

实例

  1. a = 4
  2. b = 4
  3. c = [1, 2]
  4. d = [1, 2]
  5. e = (1, 2)
  6. f = 5
  7. print(id(a))
  8. print(id(b))
  9. print(id(f))
  10. print(id(c))
  11. print(id(d))
  12. print(id(e))
  13. print(id(d[0]))
  14. print(id(d[1]))

输出

  1. 140725131924608
  2. 140725131924608
  3. 140725131924640
  4. 2708472488520
  5. 2708472488584
  6. 2708472974984
  7. 140725131924512
  8. 140725131924544

dir()

  1. 功能:函数不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。
  2. 如果参数包含方法__dir__(),该方法将被调用。如果参数不包含__dir__(),该方法将最大限度地收集参数信息。
  3. 参数:对象、变量、类型。

实例

  1. import keyword
  2. print(dir())
  3. print(dir(keyword))

输出

  1. ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__',
  2. '__spec__', 'keyword']
  3. ['__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__',
  4. 'iskeyword', 'kwlist', 'main']

发表评论

表情:
评论列表 (有 0 条评论,432人围观)

还没有评论,来说两句吧...

相关阅读