How does python find the missing parameters
This article mainly introduces python how to find the missing parameters, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Description
1. If any parameters are omitted during slicing, Python will try to calculate them automatically.
2. If you examine the source code of CPython, you will find a function called PySlice_GetIndicesEx (), which calculates the slice index of any given parameter.
It is the logical equivalent of Python.
This function uses the Python object and optional parameters to slice and returns the start, stop, step, and length of the slice.
Example
Def py_slice_get_indices_ex (obj, start=None, stop=None, step=None): length = len (obj) if step is None: step= 1 if step= = 0: raise Exception ("Step cannot be zero.") If start is None: start = 0 if step > 0 else length-1 else: if start
< 0: start += length if start < 0: start = 0 if step >0 else-1 if start > = length: start = length if step > 0 else length-1 if stop is None: stop = length if step > 0 else-1 else: if stop
< 0: stop += length if stop < 0: stop = 0 if step >0 else-1 if stop > = length: stop = length if step > 0 else length-1 if (step
< 0 and stop >= start) or (step > 0 and start > = stop): slice_length = 0 elif step < 0: slice_length = (stop-start + 1) / (step) + 1 else: slice_length = (stop-start-1) / (step) + 1 return (start, stop, step, slice_length) Thank you for reading this article carefully I hope the article "how to find the missing parameters in python" shared by the editor is helpful to everyone. At the same time, I also hope that you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!