LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-25-2014, 12:03 PM   #1
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
python reverse function


Is this the way reverse is supposed to work in python?

Quote:
> python
Python 2.7.3 (default, Feb 27 2014, 19:37:34)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = [1,2,3]
>>> mylist
[1, 2, 3]
>>> copylist = mylist
>>> copylist
[1, 2, 3]
>>> copylist.reverse()
>>> mylist
[3, 2, 1]
>>>
Why does reversing the copy cause the original to get reversed?
 
Old 06-25-2014, 12:26 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,224

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
It's not a matter of how reversing works. It's a matter of how assignments work.

Code:
# a refers to the location in memory where the list is 
a = [1, 2, 3]

# b refers to the same memory location as a
b = a

# the list at the memory location referred to by a (and therefore b) is reversed
a.reverse()
If you want b to be a copy of a, you need to specify that explicity. It's a list of integers, so we can use what's called a "shallow copy":

Code:
import copy
a = [1, 2, 3]
b = copy.copy(a)
b.reverse()
Note: this is actually fairly consistent with how it works in most other languages. "Complex types", such as ArrayLists, work like this in C# and Java, and you'd usually use pointers and/or references in C/C++ to get equivalent behavior. You get better performance that way.

Last edited by dugan; 06-25-2014 at 12:33 PM.
 
Old 06-25-2014, 01:05 PM   #3
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Yep. You're right.

Verified it with another function.

Quote:
> python
Python 2.7.3 (default, Feb 27 2014, 19:37:34)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = [1,2,3]
>>> copylist = mylist
>>> copylist.append(4)
>>> mylist
[1, 2, 3, 4]
I'm not sure I agree it should work this way but I understand what's going on now.

Thanks for the tip on shallow copy.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C function to reverse the byte order in a double? sneakyimp Programming 3 08-12-2010 11:24 AM
[SOLVED] Python: How to get the return value of the last function? ryan858 Programming 2 06-14-2010 04:27 AM
can someone explain the 'for' function in python to me deathalele Programming 8 10-16-2008 11:10 AM
python, os.system() function. howto use python variables? jhwilliams Programming 5 07-28-2007 01:56 AM
Keyboard and Mouse reverse and don't function after Core 2 install...HELP! motionstorm Fedora 0 04-25-2004 05:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration