5 Replies - 606 Views - Last Post: 02 January 2017 - 11:51 PM Rate Topic: -----

#1 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 411
  • View blog
  • Posts: 1,319
  • Joined: 27-December 13

Why is SCOPE not isolated in this function?

Posted 02 January 2017 - 03:15 PM

Look at this simple program:
def rev(a, c):
    a.reverse()
    c.reverse()


number1 = [int(x) for x in input("First number: ")]
number2 = [int(x) for x in input("Second number ")]

print(number1, number2)

rev(number1, number2)

print(number1, number2)


# DATA:
First number: 123
Second number 1234
[1, 2, 3] [1, 2, 3, 4]
[3, 2, 1] [4, 3, 2, 1]


The code takes two numbers and split them into individual digits in two lists.
The two lists are then send to the function rev().
Inside the function the lists are reversed in-place.
Nothing is returned.
I would expect the reversed lists to be isolated to the scope of the function.
But, as the second print statement shows, the lists are reversed even in the main.
Why is this?

Is This A Good Question/Topic? 0
  • +

Replies To: Why is SCOPE not isolated in this function?

#2 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 411
  • View blog
  • Posts: 1,319
  • Joined: 27-December 13

Re: Why is SCOPE not isolated in this function?

Posted 02 January 2017 - 03:26 PM

Probably the in-place handling do not assign anything, just modifies the existing lists.
ok - I am tired...
Was This Post Helpful? 0
  • +
  • -

#3 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Why is SCOPE not isolated in this function?

Posted 02 January 2017 - 03:41 PM

I always though of object handling in Python like passing references in C++, but perhaps that's to simplistic?

At least in this case, that concept seems to hold up. References to the original are being passed around. Not until you either explicitly copy or re-assign the whole variable does it stop operating on the original object.
Was This Post Helpful? 0
  • +
  • -

#4 CurlyJoe   User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 97
  • Joined: 19-September 13

Re: Why is SCOPE not isolated in this function?

Posted 02 January 2017 - 10:47 PM

Lists are mutable so the function is operating on the same block of memory (note the no return from reverse()=uses the same memory location). If you do not want this, send a copy of the list to the function --> rev(number1[:], number2[:]) Also, tuples, strings, ints, etc. are not mutable=new memory location when they are changed, so they have to be returned from a function. When you get into things like multiprocessing, and you want to communicate to-from-between threads, it requires a mutable object so everything is accessing the same block of memory. A simple explanation https://codehabitude...e-vs-immutable/

This post has been edited by CurlyJoe: 02 January 2017 - 10:58 PM

Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11095
  • View blog
  • Posts: 18,982
  • Joined: 19-March 11

Re: Why is SCOPE not isolated in this function?

Posted 02 January 2017 - 11:12 PM

Quote

The code takes two numbers and split them into individual digits in two lists.
The two lists are then send to the function rev().
Inside the function the lists are reversed in-place.
Nothing is returned.
I would expect the reversed lists to be isolated to the scope of the function.
But, as the second print statement shows, the lists are reversed even in the main.
Why is this?


list.reverse() changes the state of the list object it's called on and does not return a result. You can use the reversed "function" (it's actually a constructor, don't sweat that now) if you want something that acts more or less like what you're wanting list.reverse() to do for you. (it returns an object which, for all practical purposes, is the list you gave it originally, but reversed, and does not change the original list)
Was This Post Helpful? 0
  • +
  • -

#6 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 411
  • View blog
  • Posts: 1,319
  • Joined: 27-December 13

Re: Why is SCOPE not isolated in this function?

Posted 02 January 2017 - 11:51 PM

Thank you to Atli, CurlyJoe and jon.kiparsky.
I understand, but still find it a bit surprising that function arguments can be modified globally in this way.
But now I know, and will hopefully not land in this pitfall again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1