|
65 | 65 | "In [1]: help(len)\n", |
66 | 66 | "Help on built-in function len in module builtins:\n", |
67 | 67 | "\n", |
68 | | - "len(...)\n", |
69 | | - " len(object) -> integer\n", |
70 | | - " \n", |
71 | | - " Return the number of items of a sequence or mapping.\n", |
| 68 | + "len(obj, /)\n", |
| 69 | + " Return the number of items in a container.\n", |
72 | 70 | "```\n", |
73 | 71 | "\n", |
74 | 72 | "Depending on your interpreter, this information may be displayed as inline text, or in some separate pop-up window." |
|
82 | 80 | "\n", |
83 | 81 | "```ipython\n", |
84 | 82 | "In [2]: len?\n", |
85 | | - "Type: builtin_function_or_method\n", |
86 | | - "String form: <built-in function len>\n", |
87 | | - "Namespace: Python builtin\n", |
88 | | - "Docstring:\n", |
89 | | - "len(object) -> integer\n", |
90 | | - "\n", |
91 | | - "Return the number of items of a sequence or mapping.\n", |
| 83 | + "Signature: len(obj, /)\n", |
| 84 | + "Docstring: Return the number of items in a container.\n", |
| 85 | + "Type: builtin_function_or_method\n", |
92 | 86 | "```" |
93 | 87 | ] |
94 | 88 | }, |
|
101 | 95 | "```ipython\n", |
102 | 96 | "In [3]: L = [1, 2, 3]\n", |
103 | 97 | "In [4]: L.insert?\n", |
104 | | - "Type: builtin_function_or_method\n", |
105 | | - "String form: <built-in method insert of list object at 0x1024b8ea8>\n", |
106 | | - "Docstring: L.insert(index, object) -- insert object before index\n", |
| 98 | + "Signature: L.insert(index, object, /)\n", |
| 99 | + "Docstring: Insert object before index.\n", |
| 100 | + "Type: builtin_function_or_method\n", |
107 | 101 | "```\n", |
108 | 102 | "\n", |
109 | 103 | "or even objects themselves, with the documentation from their type:\n", |
|
113 | 107 | "Type: list\n", |
114 | 108 | "String form: [1, 2, 3]\n", |
115 | 109 | "Length: 3\n", |
116 | | - "Docstring:\n", |
117 | | - "list() -> new empty list\n", |
118 | | - "list(iterable) -> new list initialized from iterable's items\n", |
| 110 | + "Docstring: \n", |
| 111 | + "Built-in mutable sequence.\n", |
| 112 | + "\n", |
| 113 | + "If no argument is given, the constructor creates a new empty list.\n", |
| 114 | + "The argument must be an iterable if specified.\n", |
119 | 115 | "```" |
120 | 116 | ] |
121 | 117 | }, |
|
145 | 141 | "\n", |
146 | 142 | "```ipython\n", |
147 | 143 | "In [7]: square?\n", |
148 | | - "Type: function\n", |
149 | | - "String form: <function square at 0x103713cb0>\n", |
150 | | - "Definition: square(a)\n", |
151 | | - "Docstring: Return the square of a.\n", |
| 144 | + "Signature: square(a)\n", |
| 145 | + "Docstring: Return the square of a.\n", |
| 146 | + "File: <ipython-input-6>\n", |
| 147 | + "Type: function\n", |
152 | 148 | "```\n", |
153 | 149 | "\n", |
154 | 150 | "This quick access to documentation via docstrings is one reason you should get in the habit of always adding such inline documentation to the code you write!" |
|
164 | 160 | "\n", |
165 | 161 | "```ipython\n", |
166 | 162 | "In [8]: square??\n", |
167 | | - "Type: function\n", |
168 | | - "String form: <function square at 0x103713cb0>\n", |
169 | | - "Definition: square(a)\n", |
170 | | - "Source:\n", |
| 163 | + "Signature: square(a)\n", |
| 164 | + "Source: \n", |
171 | 165 | "def square(a):\n", |
172 | | - " \"Return the square of a\"\n", |
| 166 | + " \"\"\"Return the square of a.\"\"\"\n", |
173 | 167 | " return a ** 2\n", |
| 168 | + "File: <ipython-input-6>\n", |
| 169 | + "Type: function\n", |
174 | 170 | "```\n", |
175 | 171 | "\n", |
176 | 172 | "For simple functions like this, the double question-mark can give quick insight into the under-the-hood details." |
|
186 | 182 | "\n", |
187 | 183 | "```ipython\n", |
188 | 184 | "In [9]: len??\n", |
189 | | - "Type: builtin_function_or_method\n", |
190 | | - "String form: <built-in function len>\n", |
191 | | - "Namespace: Python builtin\n", |
192 | | - "Docstring:\n", |
193 | | - "len(object) -> integer\n", |
194 | | - "\n", |
195 | | - "Return the number of items of a sequence or mapping.\n", |
| 185 | + "Signature: len(obj, /)\n", |
| 186 | + "Docstring: Return the number of items in a container.\n", |
| 187 | + "Type: builtin_function_or_method\n", |
196 | 188 | "```\n", |
197 | 189 | "\n", |
198 | 190 | "Using ``?`` and/or ``??`` gives a powerful and quick interface for finding information about what any Python function or module does." |
|
220 | 212 | "\n", |
221 | 213 | "```ipython\n", |
222 | 214 | "In [10]: L.<TAB>\n", |
223 | | - "L.append L.copy L.extend L.insert L.remove L.sort \n", |
224 | | - "L.clear L.count L.index L.pop L.reverse \n", |
| 215 | + " append() count insert reverse \n", |
| 216 | + " clear extend pop sort \n", |
| 217 | + " copy index remove \n", |
225 | 218 | "```\n", |
226 | 219 | "\n", |
227 | 220 | "To narrow-down the list, you can type the first character or several characters of the name, and the Tab key will find the matching attributes and methods:\n", |
228 | 221 | "\n", |
229 | 222 | "```ipython\n", |
230 | 223 | "In [10]: L.c<TAB>\n", |
231 | | - "L.clear L.copy L.count \n", |
| 224 | + " clear() count()\n", |
| 225 | + " copy() \n", |
232 | 226 | "\n", |
233 | 227 | "In [10]: L.co<TAB>\n", |
234 | | - "L.copy L.count \n", |
| 228 | + " copy() count()\n", |
235 | 229 | "```\n", |
236 | 230 | "\n", |
237 | 231 | "If there is only a single option, pressing the Tab key will complete the line for you.\n", |
|
247 | 241 | "\n", |
248 | 242 | "```ipython\n", |
249 | 243 | "In [10]: L._<TAB>\n", |
250 | | - "L.__add__ L.__gt__ L.__reduce__\n", |
251 | | - "L.__class__ L.__hash__ L.__reduce_ex__\n", |
| 244 | + " __add__ __delattr__ __eq__ \n", |
| 245 | + " __class__ __delitem__ __format__()\n", |
| 246 | + " __class_getitem__() __dir__() __ge__ >\n", |
| 247 | + " __contains__ __doc__ __getattribute__ \n", |
252 | 248 | "```\n", |
253 | 249 | "\n", |
254 | | - "For brevity, we've only shown the first couple lines of the output.\n", |
| 250 | + "For brevity, we've only shown the first few columns of the output.\n", |
255 | 251 | "Most of these are Python's special double-underscore methods (often nicknamed \"dunder\" methods)." |
256 | 252 | ] |
257 | 253 | }, |
|
265 | 261 | "Here we'll use it to find all possible imports in the ``itertools`` package that start with ``co``:\n", |
266 | 262 | "```\n", |
267 | 263 | "In [10]: from itertools import co<TAB>\n", |
268 | | - "combinations compress\n", |
269 | | - "combinations_with_replacement count\n", |
| 264 | + " combinations() compress()\n", |
| 265 | + " combinations_with_replacement() count()\n", |
270 | 266 | "```\n", |
271 | 267 | "Similarly, you can use tab-completion to see which imports are available on your system (this will change depending on which third-party scripts and modules are visible to your Python session):\n", |
272 | 268 | "```\n", |
273 | 269 | "In [10]: import <TAB>\n", |
274 | | - "Display all 399 possibilities? (y or n)\n", |
275 | | - "Crypto dis py_compile\n", |
276 | | - "Cython distutils pyclbr\n", |
277 | | - "... ... ...\n", |
278 | | - "difflib pwd zmq\n", |
| 270 | + " abc anyio \n", |
| 271 | + " activate_this appdirs \n", |
| 272 | + " aifc appnope >\n", |
| 273 | + " antigravity argon2 \n", |
279 | 274 | "\n", |
280 | 275 | "In [10]: import h<TAB>\n", |
281 | | - "hashlib hmac http \n", |
282 | | - "heapq html husl \n", |
283 | | - "```\n", |
284 | | - "(Note that for brevity, I did not print here all 399 importable packages and modules on my system.)" |
| 276 | + " hashlib html \n", |
| 277 | + " heapq http \n", |
| 278 | + " hmac \n", |
| 279 | + "```" |
285 | 280 | ] |
286 | 281 | }, |
287 | 282 | { |
|
311 | 306 | "We can search for it this way:\n", |
312 | 307 | "\n", |
313 | 308 | "```ipython\n", |
314 | | - "In [10]: str.*find*?\n", |
| 309 | + "In [11]: str.*find*?\n", |
315 | 310 | "str.find\n", |
316 | 311 | "str.rfind\n", |
317 | 312 | "```\n", |
318 | 313 | "\n", |
319 | | - "I find this type of flexible wildcard search can be very useful for finding a particular command when getting to know a new package or reacquainting myself with a familiar one." |
| 314 | + "I find this type of flexible wildcard search can be useful for finding a particular command when getting to know a new package or reacquainting myself with a familiar one." |
320 | 315 | ] |
321 | 316 | }, |
322 | 317 | { |
|
0 commit comments