473,663 Members | 2,864 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

two quick questions

Two quick newbie questions:

1) Does Python have passing-by-reference?
2) In ordinary parlance, "deep" implies "shallow" but not conversely. In the
Python "copy" module (if I understand correctly), the implication goes the other
way. Do you find this a nuisance?

Peace,
EJ
Jul 18 '05 #1
7 2070
Elaine Jackson wrote:
1) Does Python have passing-by-reference?
It depends on exactly what you mean by that. In a sense all Python
objects are passed by reference, but only in the sense that the
reference is passed by value. (Say that three times fast.)

If you want to get the equivalent of a C++ reference on an immutable
object, you can do it with containment. Pass the function a mutable
container containing your object, and then manipulate/change the
contained object. In the caller's scope, the container will have
mutated.
2) In ordinary parlance, "deep" implies "shallow" but not conversely.
In the
Python "copy" module (if I understand correctly), the implication goes
the other
way. Do you find this a nuisance?


I'm not sure what about the copy's modules semantics you're thinking are
reversed, but the terminology used in the copy module is common in
computer science. A shallow copy means that the object is copied, but
it will retain the same references to contained objects; a deep copy
means that the object is copied, as well as the objects it contains (and
so on, recursively). A deep copy always does the same thing as a
shallow copy, and more.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ I always entertain great hopes.
\__/ Robert Frost
Jul 18 '05 #2
>>>>> "Elaine" == Elaine Jackson <el************ ***@home.com> writes:
Two quick newbie questions:
1) Does Python have passing-by-reference?
Python only has "passing-by-value". However, in Python you always work with
references to objects. So in Python function calls pass references by
value. Hope that makes sense :-).
2) In ordinary parlance, "deep" implies "shallow" but not conversely. In
the Python "copy" module (if I understand correctly), the implication goes
the other way. Do you find this a nuisance?


If I understand you correctly, "deep" does imply "shallow" in the "copy"
module. Perhaps you can point to documentation that led you believe it
"goes the other way".

Ganesan

--
Ganesan R

Jul 18 '05 #3

"Elaine Jackson" <el************ ***@home.com> wrote in message
news:uz******** *************** @news1.calgary. shaw.ca...
Two quick newbie questions:

1) Does Python have passing-by-reference?
Python no. Arg passing is by object binding. CPython uses *PyObject
passing to implement this. Human readers do what they do. For more,
try to find long thread on function calls/arg passing earlier this
year (via Google).

You should ask yourself why you ask this, and you might get answer
more directly relevant to you.
2) In ordinary parlance, "deep" implies "shallow" but not conversely. In the Python "copy" module (if I understand correctly), the implication goes the other way. Do you find this a nuisance?


I believe deep copy does shallow copy + more copy so that 'deep'
*does* imply 'shallow'. For this sort of question, start interpreter
in interactive mode (or use IDE that simulates this mode), make up
simple example, import copy module, and interactively experiment.
This is best way to learn actual behavior. Ability to do so is great
feature of Python.

Terry J. Reedy
Jul 18 '05 #4
The following examples might clear the more rheoretical elaborations .....
def noUse(a):
a=(4,5,6)

def tricky(a):
a[0]=(7,8,9)

# case 1
x=[1,2,3]
print x
tricky(x)

x=(1,2,3)
# case 2
noUse ([x])
print x

# case 3
tricky([x])
print x

# case 4
y=[x]
tricky (y)
print x
print y[0]

# case 5
tricky(x)
print x
Kindly
Michael Peuser

"Erik Max Francis" <ma*@alcyone.co m> schrieb im Newsbeitrag
news:3F******** *******@alcyone .com...
Elaine Jackson wrote:
1) Does Python have passing-by-reference?


It depends on exactly what you mean by that. In a sense all Python
objects are passed by reference, but only in the sense that the
reference is passed by value. (Say that three times fast.)

If you want to get the equivalent of a C++ reference on an immutable
object, you can do it with containment. Pass the function a mutable
container containing your object, and then manipulate/change the
contained object. In the caller's scope, the container will have
mutated.
2) In ordinary parlance, "deep" implies "shallow" but not conversely.
In the
Python "copy" module (if I understand correctly), the implication goes
the other
way. Do you find this a nuisance?


I'm not sure what about the copy's modules semantics you're thinking are
reversed, but the terminology used in the copy module is common in
computer science. A shallow copy means that the object is copied, but
it will retain the same references to contained objects; a deep copy
means that the object is copied, as well as the objects it contains (and
so on, recursively). A deep copy always does the same thing as a
shallow copy, and more.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ I always entertain great hopes.
\__/ Robert Frost

Jul 18 '05 #5
Elaine Jackson wrote:
Two quick newbie questions:

1) Does Python have passing-by-reference?


There have been lots of interesting discussions about this in the past. Here's
one such thread:

http://groups.google.com/groups?thre...ost.accu.uu.nl

HTH,

Jul 18 '05 #6
On Tue, 2003-08-12 at 23:06, Elaine Jackson wrote:
1) Does Python have passing-by-reference?
Yes. But the references you are passing are references to objects (not
memory locations), and objects themselves can be changeable (mutable) or
not. When you pass objects, copies are not automatically made (so
assignment is very speedy).

I prefer to say that these concepts should be put aside when thinking
about python.

Python has names that refer to objects. Objects are created, and can be
assigned one or more names (using assignment). Some objects can mutate
(like objects made by user defined classes), some cannot (like strings
or integers).

When you supply function arguments, copies of the object are not
automatically made. If you pass a mutable object, the callee has a name
for that (actual) object, and can mutate it. If you pass an immutable
object, they cannot.

When you use assignment, a copy of the object is not made. It simply
gets another name that refers to it.

Globals can confuse the issue, since they allow you to change the names
used in the global scope (ie. they can allow your function to modify a
non-local namespace, rather than just the objects passed in to the local
namespace.

Examples:

1) Without using globals:

a = 1 # Create the immutable 1 integer object and name it 'a'
b = [] # Create a mutable list

def f( c, d ):
c = 3 # Reassign name 'c' to the 3 integer object
d.append( "foo" ) # modify the list that is named 'd'

f( a, b ) # Copies of 'a', and 'b' are NOT made.
# when you call f(), c is a, and d is b.
# ie. The names in the function refer to the same
# objects as the (different) names outside the function

a == 1
b == ['foo']

Discussion:
The f() function changed 'b' because the object itself was changeable.
The a object is still 1, because the 1 object cannot
be changed at all, and f() couldn't reassign a because it gets a name
that refers to the object, not access to the namespace itself.
2) similar example using globals to pervert namespace

a = 1

def g():
global a # This says that 'a' refers to the global 'a' name
a = 2 # Since I have access to the name 'a', I can change
# the object that the global 'a' refers to.

g()
a == 2 # After calling g, the global name 'a' was changed.
The point of the globals example, was that it can be used to confuse
your understanding. So ignore it for now, and think of objects as
free-standing entities that can have multiple names, in multiple scopes,
it is easier to understand that you are passing around access to those
objects, and you can manipulate them if they are mutable. But,
pass-by-value and pass-by-reference, at least as they are typically
discussed in the 'C' programming world, are less applicable concepts.

As you get more advanced, you will see that Python uses dictionaries to
hold namespaces, and you can pass thos dictionaries (and thus the
namespaces) around as well.

2) In ordinary parlance, "deep" implies "shallow" but not conversely. In the
Python "copy" module (if I understand correctly), the implication goes the other
way. Do you find this a nuisance?


Not sure what you mean.

--
Chad Netzer
Jul 18 '05 #7
Thanks to everyone who responded about this, both for the info regarding
question (1) and for the tact in not balaboring the sheer stupidity behind
question (2). That whole business got inverted somehow on its way to (whatever
passes for) my brain. Sorry about that. On the up side, it may turn out to have
been an instructive mistake: both questions are special cases of a single
underlying question or problem that I've been harboring for some time. I may
eventually start a thread about it (here or elsewhere), but for now I'm still
trying to mould it into a sensible question. I think it could be an interesting
topic for people concerned with computer-science pedagogy. So far I don't even
know if there's anyone like that around here.

In any case, mucho appreciado for the help.

ej

=============== =============== ==

Elaine Jackson <el************ ***@home.com> wrote in message
news:uz******** *************** @news1.calgary. shaw.ca...
| Two quick newbie questions:
|
| 1) Does Python have passing-by-reference?
| 2) In ordinary parlance, "deep" implies "shallow" but not conversely. In the
| Python "copy" module (if I understand correctly), the implication goes the
other
| way. Do you find this a nuisance?
|
| Peace,
| EJ
|
|
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
8436
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8858
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8548
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8634
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4182
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4349
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.