473,385 Members | 1,630 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,385 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 1386
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
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.