473,231 Members | 2,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 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 1383
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

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

Similar topics

1
by: DaRik | last post by:
Hello, I'm putting together a menu with explorer like maps and I need to pass a reference to the current node to another function that will change the appearance of the map. In the js I'm...
3
by: Alex | last post by:
When I pass a reference to a method by value, the reference argument to the method is copied but still points to the original object, and that method can modify the object. Can I apply const...
1
by: Charlie | last post by:
Hi: My project has a seperate DLL for biz logic and an EXE for client app. I would like to pass in a reference to a form in client app to DLL to capture form element data for updating. In my...
6
by: Lenn | last post by:
Hi, Could someone clarify my confusion regarding passing reference types to a method with ref keyword and explain when it's practical to use it. It's my understanding that in .NET reference...
13
by: Maxim | last post by:
Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }
3
by: John | last post by:
Hi I need to do the same process on separate classes. Is there a way to pass reference of different classes to the same procedure? Thanks Regards
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
7
by: Johannes Bauer | last post by:
Hello Group, please consider the following code #include <vector> #include <iostream> #define USE_CONST #define USE_STRING
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.