|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Analyzing the behaviour of Python function slice" |
| 4 | +date: "2018-09-29 10:17:46 +0530" |
| 5 | +tag: |
| 6 | + - python |
| 7 | + - slice |
| 8 | + - python trick questions |
| 9 | +--- |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +Last Friday, I was sitting in one of the good coffee shops in Bangalore with my |
| 15 | +friend. Coffee and discussion is the best combination to release stress. It was |
| 16 | +looking like a perfect Friday evening until my friend was struck by an idea of |
| 17 | +asking me a question. |
| 18 | + |
| 19 | +“Let me conduct a quiz.” he said, interrupting our conversation. |
| 20 | + |
| 21 | +“A quiz? Quiz on what?”, I asked. |
| 22 | + |
| 23 | +“On programming”, he said |
| 24 | + |
| 25 | +“What is the level of difficulty then?” I said. |
| 26 | + |
| 27 | +Asking the level of difficulty is important. I never invest my efforts in |
| 28 | +solving something easy. If he had said easy, I would have ignored to answer, but |
| 29 | +he said “It is a bit difficult, but not that difficult. I gave a wrong answer to |
| 30 | +this question in my last interview.” |
| 31 | + |
| 32 | +There was no reason to go back from here. Taking some deep breaths I said, |
| 33 | +“Please go ahead.” |
| 34 | + |
| 35 | +He stood up, took a tissue paper from a nearby counter and scratched below code |
| 36 | +on it. [^1] And he asked me by pointing towards that code, “What will be the output |
| 37 | +of this code?” |
| 38 | + |
| 39 | +```python |
| 40 | +def my_function(): |
| 41 | + l = [0, 1, 2] |
| 42 | + print(l[30:]) |
| 43 | + |
| 44 | +my_function() |
| 45 | +``` |
| 46 | + |
| 47 | +Now it was my turn to give the answer. I looked at the code and tried parsing it |
| 48 | +in my head --- line by line. In my mind, I observed the first line. It was |
| 49 | +defining a function which seems to be correct. I moved my eyes to the next line. |
| 50 | +It was defining a variable `l` of type `list` and assigning values ranging from |
| 51 | +`0` to `2`. Even this wasn’t looking problematic. So I forwarded to the next |
| 52 | +line where it was trying to print that variable `l` by slicing it from starting |
| 53 | +value `30` to the infinity. |
| 54 | + |
| 55 | +“Well, the start value is `30` which is greater than the length of the list. |
| 56 | +This should raise an `IndexError`” I said in my mind. I was about to speak an |
| 57 | +answer, but suddenly Devil of me flashed. |
| 58 | + |
| 59 | +“It is less than [a banana job][4] my dear,” the Devil said to me, “You should |
| 60 | +take a little advantage of this opportunity my boy.” |
| 61 | + |
| 62 | +Because things were looking in my control, I shook my hands with the Devil. |
| 63 | + |
| 64 | +I said to my friend, “How about betting for some real values?” |
| 65 | + |
| 66 | +Going closer I spoke, “If I answer correctly, You will pay the bill and If I am |
| 67 | +wrong, This will be a treat from my side.” |
| 68 | + |
| 69 | +He thought for a while and nodded. Now it was my turn to unveil the cards. |
| 70 | + |
| 71 | +I said in a strong voice, “It will raise an `IndexError`.” And shifted my focus |
| 72 | +towards the chocolate. |
| 73 | + |
| 74 | +He starred my face for a second and spoke, “Okay. Are you sure about this?”. |
| 75 | + |
| 76 | +This was the hint he gave. I should have taken another shot here. What happens |
| 77 | +next become a lesson for me. |
| 78 | + |
| 79 | +I said with a flat face, “Yes I am.” |
| 80 | + |
| 81 | +With my answer, he instantly opened his backpack, took his Laptop out and typed |
| 82 | +the code which he wrote on that tissue. |
| 83 | + |
| 84 | +When I stopped hearing a sound of typing I yelled, “So did I win?” |
| 85 | + |
| 86 | +He turned his laptop towards me and exclaimed, “Not at all!” |
| 87 | + |
| 88 | +When I focused on the screen, the interpreter was printing `[]`. Damn! I lost |
| 89 | +the bet. Why the hell `slice` is returning an empty `list` even when we are |
| 90 | +trying to slice it with a value which is greater than the length of it! It was |
| 91 | +surely looking unpythonic behavior. I paid whatever the bill amount was. Entire |
| 92 | +evening this question was all roaming my mind. After coming home, I decided to |
| 93 | +justify reasons for returning an empty list instead of raising an `IndexError` |
| 94 | +from a `slice`. |
| 95 | + |
| 96 | +Below are a few reasons justifying such behavior of slice function. I am sharing |
| 97 | +this with you so that you don’t lose a bet with your friend :) For those who |
| 98 | +haven’t used slice anytime in their life, I advise to read [this][1] tutorial. |
| 99 | +Reading [this][2] guide for understanding how a slice function converts the |
| 100 | +input values. Especially rule number 3 and 4 referenced there. |
| 101 | + |
| 102 | +* **Reason number one:** |
| 103 | + |
| 104 | + Python lists are more commonly used in iterations. Consider below example: |
| 105 | + |
| 106 | + ```python |
| 107 | + numbers = [0, 1, 2, 3] |
| 108 | + for number in numbers[30:]: |
| 109 | + print(number) |
| 110 | + ``` |
| 111 | + |
| 112 | + If `slice` was raising an `IndexError`, then the above code would have to |
| 113 | + written like this |
| 114 | + |
| 115 | + written like like this |
| 116 | + |
| 117 | + ```python |
| 118 | + numbers = [0, 1, 2, 3] |
| 119 | + try: |
| 120 | + for number in numbers[30:]: |
| 121 | + print(numbers) |
| 122 | + except IndexError: |
| 123 | + pass |
| 124 | + ``` |
| 125 | + |
| 126 | + Or in another way below is also looking reasonable |
| 127 | + |
| 128 | + ```python |
| 129 | + numbers = [0, 1, 2, 3] |
| 130 | + start = 30 |
| 131 | + if start < len(numbers): |
| 132 | + for number in numbers[start:]: |
| 133 | + print(number) |
| 134 | + ``` |
| 135 | + |
| 136 | + Both the approaches are looking little lengthy by an obvious reason. And that |
| 137 | + reason is to prevent executing loop if there are no elements in it. When we |
| 138 | + observe the behavior of `slice` called at `for in`, it makes sense to return |
| 139 | + an empty list instead of raising an `IndexError`. |
| 140 | + |
| 141 | +I am not able to find further reasons to return an empty list instead of raising |
| 142 | +the `IndexError`. But I am sure, there will be. If you know any other potential |
| 143 | +reasons for such behavior of `slice`, please drop me a mail at **jaysinhp** at |
| 144 | +**gmail** dot **com** or contact me over Twitter [@jaysinhp][3]. I will update |
| 145 | +the reasons at this post and give credits to you. Thanks for reading this post. |
| 146 | + |
| 147 | + |
| 148 | +###### Proofreaders: [Geoffrey Sneddon](https://github.com/gsnedders), [Elijah](https://mailto:[email protected]), [Mahendra Yadav](mailto:[email protected]), [Dhavan Vaidya](http://codingquark.com/), |
| 149 | + |
| 150 | +[1]: https://docs.python.org/3.7/tutorial/introduction.html#lists |
| 151 | +[2]: https://docs.python.org/3.7/library/stdtypes.html#sequence-types-list-tuple-range |
| 152 | +[3]: https://twitter.com/jaysinhp |
| 153 | +[4]: http://catb.org/jargon/html/O/one-banana-problem.html |
| 154 | + |
| 155 | +[^1]: I am using the word "Scratch" because that tissue paper was such a thin that writing by a ballpen torn it. |
0 commit comments