2

could you explain to me in detail the second half of program? I understand input_list[0] = 10 is a variable where it has a range of 1 - 10, but where did the list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [10, 2, 3, 4, 5, 6, 7, 8, 9], and [10, 5, 5] come from?

def list_changer(input_list):
    input_list[0] = 10

    input_list = range(1, 10)
    print(input_list)
    input_list[0] = 10
    print(input_list)

>>> test_list = [5, 5, 5]
>>> list_changer(test_list)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print test_list
[10, 5, 5]

Thank you in advance for all of your help.

Eddy
  • 29
  • 4
  • 2
    Read about the `range()` function. It creates and returns such lists. – Alfe Apr 05 '15 at 23:41
  • One more thing you need to know is that lists are passed per reference (i. e. changes done to one are done to the original and thus have effect also after the function finished). This is why you change the `[5, 5, 5]` to a `[10, 5, 5]`. – Alfe Apr 05 '15 at 23:43
  • So if I'm understanding this correctly, the second list of numbers [10, 2, 3....] is being called which is why you start at 10, stop at 5, step are at 5. Is that the proper way of thinking? – Eddy Apr 05 '15 at 23:57
  • See [“Least Astonishment” in Python: The Mutable Default Argument](http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument) – dawg Apr 06 '15 at 00:05
  • @dawg huh? That has very little to do with the OPs code. There is no keyword argument in sight, mutable or not. – Lukas Graf Apr 06 '15 at 00:07
  • Is this really a three-upvotes-from-20-views question? – Bill Woodger Apr 06 '15 at 00:18
  • Any and every bit of info helps. I understand there's no argument stated but now I know about default parameter values. Thank you for the article, @dawg – Eddy Apr 06 '15 at 00:20
  • @Eddy: The effect demonstrated by your code comes from the fact that Python functions get passed a reference to an object, not a value. There's an excellent talk by Brandon Rhodes where he explains this concept in detail: [PyOhio 2011: Names, Objects, and Plummeting From The Cliff](http://pyvideo.org/video/542/pyohio-2011-names-objects-and-plummeting-from) It's a bit lengthy, but it's worth getting this fundamental concept right early on. – Lukas Graf Apr 06 '15 at 00:24
  • Thanks, @LukasGraf. I'm watching it right now. – Eddy Apr 06 '15 at 00:27

2 Answers2

1

Try this using id

def list_changer(input_list):
    input_list[0] = 10
    print id(input_list)
    input_list = range(1, 10)
    print(input_list)
    input_list[0] = 10
    print(input_list)


>>>test_list = [5, 5, 5]
>>>print id(test_list)
>>>list_changer(test_list)
>>>print test_list

#output

139794448752512
139794448752512
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 5, 5]

From this we can see that id of test_list is same as input_list in the 1st line of function.That is both are referencing to [5,5,5].So changes to test_list or input_list(1st line) will affect to all variables referencing it.1st line is where changes occuring for [5,5,5].

Then input_list = range(1, 10).This time input_list is referencing range(1, 10).Still [10,5,5] is referencing by variable test_list.

Hope this helps

itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
  • Thank you, this really does give me more of an insight as to where the codes are running off of. – Eddy Apr 06 '15 at 00:07
1

You should notice that are two different variables named input_list:

  1. The first is declared globally as test_list, initialized by [5,5,5] and passed as an argument with the name input_list to the function list_changer.

  2. The second is declared locally within the function list_changer, initialized by range(1,10) and overrode the name of the first one.

You can check this by printing id(input_list) each time you print the value.

Assem
  • 11,574
  • 5
  • 59
  • 97
  • Unfortunately this isn't correct. There is no global `input_list`. The variable in the global scope is named `test_list`, not `input_list`. – Lukas Graf Apr 06 '15 at 00:09
  • @LukasGraf test_list is the same as the input_list passed by argument. I didnt notice that is named differently outside,but they are the same variabe. – Assem Apr 06 '15 at 00:11
  • They end up referencing the same list, but they're not the same name. So it's not at all a scoping issue. – Lukas Graf Apr 06 '15 at 00:13