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?
9 1386
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!
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: - >>> integerList = [i for i in range(10)]
-
>>> integerList
-
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
-
>>> oddIntegerList = [i for i in range(10) if (i%2)]
-
>>> oddIntegerList
-
[1, 3, 5, 7, 9]
-
>>>
are examples of the power of comprehensions.
We'll get into generators another day.
Ok, I went ahead and did - >>> r ="test"
-
>>> def changeString(string):
-
""" Test"""
-
string = "56"
-
-
>>> print r
-
test
-
>>> changeString(r)
-
>>> print r
-
test
-
>>>
So call by value
Then I did - >>> def changeList(list1) :
-
"""list"""
-
list1.append("appended")
-
-
-
>>> b = [3]
-
>>> b
-
[3]
-
>>> changeList(b)
-
>>> b
-
[3, 'appended']
So does Python do call by value reference?
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: - >>> integerList = [i for i in range(10)]
-
>>> integerList
-
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
-
>>> oddIntegerList = [i for i in range(10) if (i%2)]
-
>>> oddIntegerList
-
[1, 3, 5, 7, 9]
-
>>>
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 - >>> x = range(10)
-
>>> x
-
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
-
But I expect there's much more to lists than just playing around with ranges and slices.
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 -
>>> a = [1,2]
-
>>> b=a[:]
-
>>> b[0]=3
-
>>> b
-
[3, 2]
-
>>> a
-
[1, 2]
-
>>> id(a)
-
-1215364148
-
>>> id(b)
-
-1215363892
-
if not, they are the same -
>>> a
-
[1, 2]
-
>>> b=a
-
>>> b
-
[1, 2]
-
>>> id(b)
-
-1215364148
-
>>> id(a)
-
-1215364148
-
you need to do a shallow copy
eg -
>>> a = [1,2]
-
>>> b=a[:]
-
>>> b[0]=3
-
>>> b
-
[3, 2]
-
>>> a
-
[1, 2]
-
>>> id(a)
-
-1215364148
-
>>> id(b)
-
-1215363892
-
if not, they are the same -
>>> a
-
[1, 2]
-
>>> b=a
-
>>> b
-
[1, 2]
-
>>> id(b)
-
-1215364148
-
>>> id(a)
-
-1215364148
-
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?
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?
- import copy
-
-
x = copy.copy(y) # make a shallow copy of y
-
x = copy.deepcopy(y) # make a deep copy of y
- import copy
-
-
x = copy.copy(y) # make a shallow copy of y
-
x = copy.deepcopy(y) # make a deep copy of y
Great. I'm in danger of beginning to like Python.
Great. I'm in danger of beginning to like Python.
You love it! You can't get enough of it!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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"
}
|
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
|
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...
|
by: Johannes Bauer |
last post by:
Hello Group,
please consider the following code
#include <vector>
#include <iostream>
#define USE_CONST
#define USE_STRING
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
| |