471,342 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,342 software developers and data experts.

Lists and passing by reference

13,262 8TB
I did
>>> a = [5, 7, "7"]
>>> b = [a, 5, 6]
>>> b[0].append(8)
>>> a
[5, 7, '7', 8]

So it seems b[0] and a actually the same object. Does that mean that Python uses pass by reference?
Jun 14 '07 #1
9 1206
bartonc
6,596 Expert 4TB
I did
>>> a = [5, 7, "7"]
>>> b = [a, 5, 6]
>>> b[0].append(8)
>>> a
[5, 7, '7', 8]

So it seems b[0] and a actually the same object. Does that mean that Python uses pass by reference?
Yes. Mutable objects, such as lists, are passed by reference.
Not ints and other immutable objects.

It's very cool to see that you are picking this up so quickly!
Jun 14 '07 #2
bartonc
6,596 Expert 4TB
list comprehension? What would it contain?
I mostly threw that in to mess with you. It's a pretty advanced topic, but basically python's syntax allows to to create lists (or virtual lists, called generators) with a for loop all on one line:
Expand|Select|Wrap|Line Numbers
  1. >>> integerList = [i for i in range(10)]
  2. >>> integerList
  3. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  4. >>> oddIntegerList = [i for i in range(10) if (i%2)]
  5. >>> oddIntegerList 
  6. [1, 3, 5, 7, 9]
  7. >>> 
are examples of the power of comprehensions.
We'll get into generators another day.
Jun 14 '07 #3
r035198x
13,262 8TB
Ok, I went ahead and did

Expand|Select|Wrap|Line Numbers
  1. >>> r ="test"
  2. >>> def changeString(string):
  3.     """ Test"""
  4.     string = "56"
  5.  
  6. >>> print r
  7. test
  8. >>> changeString(r)
  9. >>> print r
  10. test
  11. >>>  
So call by value
Then I did

Expand|Select|Wrap|Line Numbers
  1. >>> def changeList(list1) :
  2.     """list"""
  3.     list1.append("appended")
  4.  
  5.  
  6. >>> b = [3]
  7. >>> b
  8. [3]
  9. >>> changeList(b)
  10. >>> b
  11. [3, 'appended']
So does Python do call by value reference?
Jun 14 '07 #4
r035198x
13,262 8TB
I mostly threw that in to mess with you. It's a pretty advanced topic, but basically python's syntax allows to to create lists (or virtual lists, called generators) with a for loop all on one line:
Expand|Select|Wrap|Line Numbers
  1. >>> integerList = [i for i in range(10)]
  2. >>> integerList
  3. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  4. >>> oddIntegerList = [i for i in range(10) if (i%2)]
  5. >>> oddIntegerList 
  6. [1, 3, 5, 7, 9]
  7. >>> 
are examples of the power of comprehensions.
We'll get into generators another day.
Oh I can do that. See your number 1 is even longer than mine which is
Expand|Select|Wrap|Line Numbers
  1. >>> x = range(10)
  2. >>> x
  3. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  4.  
But I expect there's much more to lists than just playing around with ranges and slices.
Jun 14 '07 #5
ghostdog74
511 Expert 256MB
I did
>>> a = [5, 7, "7"]
>>> b = [a, 5, 6]
>>> b[0].append(8)
>>> a
[5, 7, '7', 8]

So it seems b[0] and a actually the same object. Does that mean that Python uses pass by reference?
you need to do a shallow copy
eg
Expand|Select|Wrap|Line Numbers
  1. >>> a = [1,2]
  2. >>> b=a[:]
  3. >>> b[0]=3
  4. >>> b
  5. [3, 2]
  6. >>> a
  7. [1, 2]
  8. >>> id(a)
  9. -1215364148
  10. >>> id(b)
  11. -1215363892
  12.  
if not, they are the same
Expand|Select|Wrap|Line Numbers
  1. >>> a
  2. [1, 2]
  3. >>> b=a
  4. >>> b
  5. [1, 2]
  6. >>> id(b)
  7. -1215364148
  8. >>> id(a)
  9. -1215364148
  10.  
Jun 14 '07 #6
r035198x
13,262 8TB
you need to do a shallow copy
eg
Expand|Select|Wrap|Line Numbers
  1. >>> a = [1,2]
  2. >>> b=a[:]
  3. >>> b[0]=3
  4. >>> b
  5. [3, 2]
  6. >>> a
  7. [1, 2]
  8. >>> id(a)
  9. -1215364148
  10. >>> id(b)
  11. -1215363892
  12.  
if not, they are the same
Expand|Select|Wrap|Line Numbers
  1. >>> a
  2. [1, 2]
  3. >>> b=a
  4. >>> b
  5. [1, 2]
  6. >>> id(b)
  7. -1215364148
  8. >>> id(a)
  9. -1215364148
  10.  
I guess the parameter passing methods are no different from those in Java then. How would you shallow copy a general object not neccessarily a list?
Jun 14 '07 #7
bvdet
2,851 Expert Mod 2GB
I guess the parameter passing methods are no different from those in Java then. How would you shallow copy a general object not neccessarily a list?
Expand|Select|Wrap|Line Numbers
  1. import copy
  2.  
  3. x = copy.copy(y)        # make a shallow copy of y
  4. x = copy.deepcopy(y)    # make a deep copy of y
Jun 14 '07 #8
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. import copy
  2.  
  3. x = copy.copy(y)        # make a shallow copy of y
  4. x = copy.deepcopy(y)    # make a deep copy of y
Great. I'm in danger of beginning to like Python.
Jun 14 '07 #9
Motoma
3,237 Expert 2GB
Great. I'm in danger of beginning to like Python.
You love it! You can't get enough of it!
Jun 14 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Charlie | last post: by
3 posts views Thread by John | last post: by
7 posts views Thread by Johannes Bauer | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.