# Why is SCOPE not isolated in this function?

Page 1 of 1

## 5 Replies - 606 Views - Last Post: 02 January 2017 - 11:51 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=400370&amp;s=b84bf2ca5033548063adfa47b784069b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 DK3250

• Pythonian

Reputation: 411
• 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

• Pythonian

Reputation: 411
• 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...

### #3 Atli

Reputation: 4240
• 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.

### #4 CurlyJoe

Reputation: 22
• 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

### #5 jon.kiparsky

• Beginner

Reputation: 11095
• 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)

### #6 DK3250

• Pythonian

Reputation: 411
• 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.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }