473,545 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unexpected behavior: did i create a pointer?

gu
hi to all!
after two days debugging my code, i've come to the point that the
problem was caused by an unexpected behaviour of python. or by lack of
some information about the program, of course! i've stripped down the
code to reproduce the problem:

<code>
a = {}

for x in range(10):
for y in range(10):
a[x,y] = "0"

copyOfA = a

def functionA(x,y):
print a[x,y],
copyOfA[x,y] = "*"
print a[x,y],copyOfA[x,y]
for x in range(10):
for y in range(10):
functionA(x,y)

</code>
now, in the second "for" cycle and in functionA() i only 'touch' copyOfA
(altering it). as i don't touch the variable "a", i expect it not to be
affected by any change, but copyOfA acts like a pointer to a and
altering copyOfA's values result in altering the values of "a", so the
result that i expect is:
0 0 *
0 0 *
0 0 *
0 0 *
[..]

but i get:
0 * *
0 * *
0 * *
0 * *
[..]

what's going on?
thanks in advance.
Sep 7 '07 #1
23 1682
En Fri, 07 Sep 2007 05:07:03 -0300, gu <pi********@gma il.comescribiï¿ ½:
after two days debugging my code, i've come to the point that the
problem was caused by an unexpected behaviour of python. or by lack of
some information about the program, of course! i've stripped down the
code to reproduce the problem:

<code>
a = {}

for x in range(10):
for y in range(10):
a[x,y] = "0"

copyOfA = a
copyOfA is *NOT* a copy - it's just another name pointing to the SAME
object as a.
Python will never copy anything unless told explicitely.

Read this <http://effbot.org/zone/python-objects.htm>

--
Gabriel Genellina

Sep 7 '07 #2
gu schreef:
copyOfA = a

now, in the second "for" cycle and in functionA() i only 'touch' copyOfA
(altering it).
copyOfA isn't a copy of a; it's a different name bound to the same
object as a. You can verify that: id(a) and id(copyOfA) will return the
same value.

To make a copy of a (assuming a is a dict), you can do:

copyOfA = dict(a)

or

copyOfA = a.copy()

or more generally

import copy
copyOfA = copy.copy(a)
Cheers,
Roel

--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven
Sep 7 '07 #3
gu wrote:
hi to all!
after two days debugging my code, i've come to the point that the
problem was caused by an unexpected behaviour of python. or by lack of
some information about the program, of course! i've stripped down the
code to reproduce the problem:

[snip FAQ]
Yes, basically you *created* a pointer. That's all that python has:
pointers.

When saying
>>a = AnyOldObject()
b = a
then 'a' and 'b' are different /names/ for the /very same/ object (try
"a is b", or "id(a)==id(b)") .

This is really a FAQ (once a week or so?), but for the life of me I
can't find the right words for a google query.
TO THE TROOP: What keywords would you attach to that question?

/W
Sep 7 '07 #4
On Fri, 07 Sep 2007 11:46:38 +0200, Wildemar Wildenburger wrote:
gu wrote:
>hi to all!
after two days debugging my code, i've come to the point that the
problem was caused by an unexpected behaviour of python. or by lack of
some information about the program, of course! i've stripped down the
code to reproduce the problem:

[snip FAQ]

Yes, basically you *created* a pointer. That's all that python has:
pointers.
No, you are confusing the underlying C implementation with Python. Python
doesn't have any pointers. CPython is implemented with pointers. PyPy,
being written entirely in Python, is implemented with Python objects like
lists and dicts. Jython, being implemented in Java, probably isn't
implemented with pointers either -- although of course the underlying
Java compiler might be. IronPython and Python for .Net, I have no idea
how they work. Could be magic for all I know. (Probably necromancy.)

Naturally, regardless of whether you are using CPython, IronPython, PyPy
or some other variety of Python, the objects available to you include
ints, floats, strings, lists, dicts, sets and classes... but not pointers.

Nor does it include "peek" and "poke" commands for reading and writing
into random memory locations. Python is not C, and it is not Basic, nor
is it Forth or Lisp or assembler, and you shouldn't hammer the round peg
of Python objects into the square hole of C pointers.

--
Steven.
Sep 7 '07 #5
On Fri, 07 Sep 2007 10:40:47 +0000, Steven D'Aprano wrote:
Nor does it include "peek" and "poke" commands for reading and writing
into random memory locations.
I guess `ctypes` offers tools to write `peek()` and `poke()`. :-)

Ciao,
Marc 'BlackJack' Rintsch
Sep 7 '07 #6
Am Fri, 07 Sep 2007 10:40:47 +0000 schrieb Steven D'Aprano:
Python doesn't have any pointers.
Thinking of python variables or "names" as pointers should
get you a long way when trying to understand python's behaviour.

As long as you keep in mind that python doesn't have pointers to pointers,
and no pointer arithmetic either...

Peter
Sep 7 '07 #7
On 2007-09-07, Peter Otten <__*******@web. dewrote:
Am Fri, 07 Sep 2007 10:40:47 +0000 schrieb Steven D'Aprano:
>Python doesn't have any pointers.

Thinking of python variables or "names" as pointers should
get you a long way when trying to understand python's behaviour.
But thinking of them as names bound to objects will get you
further (and get you there faster). ;)
As long as you keep in mind that python doesn't have pointers
to pointers, and no pointer arithmetic either...
--
Grant Edwards grante Yow! Hello... IRON
at CURTAIN? Send over a
visi.com SAUSAGE PIZZA! World War
III? No thanks!
Sep 7 '07 #8
Steven D'Aprano wrote:
On Fri, 07 Sep 2007 11:46:38 +0200, Wildemar Wildenburger wrote:
>gu wrote:
>>hi to all!
after two days debugging my code, i've come to the point that the
problem was caused by an unexpected behaviour of python. or by lack of
some information about the program, of course! i've stripped down the
code to reproduce the problem:

[snip FAQ]
Yes, basically you *created* a pointer. That's all that python has:
pointers.

No, you are confusing the underlying C implementation with Python.
I do not, as I have no clue of the C implementation :).
I just thought I'd go along with the analogy the OP created as that was
his mindset and it would make things easier to follow if I didn't try to
forcibly change that.
Please note that I had intended for the word 'created' to be in quotes
rather than asterisks. I bit myself after I read my mistake online but
then thought "Who cares?". I should have, maybe :).
And yes, I will admit that going along with that analogy isn't the best
way to explain it. Grant Edwards in reply to Peter Otten makes it much
clearer, I guess.

/W
Sep 7 '07 #9
Steven D'Aprano wrote:
On Fri, 07 Sep 2007 11:46:38 +0200, Wildemar Wildenburger wrote:
>gu wrote:
>>hi to all!
after two days debugging my code, i've come to the point that the
problem was caused by an unexpected behaviour of python. or by lack of
some information about the program, of course! i've stripped down the
code to reproduce the problem:

[snip FAQ]
Yes, basically you *created* a pointer. That's all that python has:
pointers.

No, you are confusing the underlying C implementation with Python. Python
doesn't have any pointers. CPython is implemented with pointers. PyPy,
being written entirely in Python, is implemented with Python objects like
lists and dicts. Jython, being implemented in Java, probably isn't
implemented with pointers either -- although of course the underlying
Java compiler might be. IronPython and Python for .Net, I have no idea
how they work. Could be magic for all I know. (Probably necromancy.)

Naturally, regardless of whether you are using CPython, IronPython, PyPy
or some other variety of Python, the objects available to you include
ints, floats, strings, lists, dicts, sets and classes... but not pointers.

Nor does it include "peek" and "poke" commands for reading and writing
into random memory locations. Python is not C, and it is not Basic, nor
is it Forth or Lisp or assembler, and you shouldn't hammer the round peg
of Python objects into the square hole of C pointers.
This seems to be obscuring the real issue for the sake of hammering an
error in vocabulary. gu said """Yes, basically you *created* a pointer.
That's all that python has: pointers.""" and you took issue with that,
ultimately saying """the objects available to you include ints, floats,
strings, lists, dicts, sets and classes... but not pointers""".

You are both right, and you are also both wrong.

Python does indeed provide a rich set of strongly typed object classes,
and so clearly to say "Python only has pointers" is strictly an error.
However, this overlooks the fact that *all* names in Python, and *all*
items in container objects (lists, tuples, dicts ...) hold references to
objects.

I mention this because it *is* significant to the semantics of
assignment. From the point of view of a C programmer Python might
actually look quite like a language in which all data objects had to be
malloc'd, and the only variables were pointers to. Assignment (binding)
in Python creates copies of references to objects, not copies of the
objects themselves.

I have deliberately omitted discussion of the automatic dereferencing
that takes place in Python, thereby allowing us to treat names as though
they contained objects, but the fact remains that names *don't* contain
objects, they contain references to objects. If you want to regard a
reference and a pointer as two different things then I guess that's your
nit to pick. But I don't think the original assertions quite justified
your scathing remarks about "peek" and "poke".

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Sep 7 '07 #10

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

Similar topics

2
10415
by: Attila Feher | last post by:
Hi all, I have not done much work around exceptions; and even when I do I avoid exception specifications. But now I have to teach people about these language facilities, so I am trying them out with gcc 3.4.2-mingw. I have tried the TCPL Spec.Ed. example of just adding std::bad_exception to the exception specification of a function...
16
2422
by: He Shiming | last post by:
Hi, I'm having a little bit of trouble regarding pointer casting in my program. I don't understand why the following two cases produce different results. Case 1: IInterface *pInterface = new CImplementation(); pInterface->Method(); Case 2:
7
1687
by: Dave Hansen | last post by:
OK, first, I don't often have the time to read this group, so apologies if this is a FAQ, though I couldn't find anything at python.org. Second, this isn't my code. I wouldn't do this. But a colleague did, got an unexpected result, and asked me why. I think I can infer what is occurring, and I was able to find a simple work-around. But...
2
2159
by: Gerhard Esterhuizen | last post by:
Hi, I am observing unexpected behaviour, in the form of a corrupted class member access, from a simple C++ program that accesses an attribute declared in a virtual base class via a chain of virtual method calls. To further complicate (or perhaps simplify) matters, some compilers (GCC and MingW) produce the expected behaviour, while others...
2
1527
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
5
1216
by: Timothy Perrigo | last post by:
This bug? feature? caused a bit of havoc for us yesterday...A reproducible example follows. Essentially, if you have a table with a primary key called "id", and you create a temp table (via a "select into") containing a subset of the data from the table but where the primary key field is renamed (in the example below, it is called "not_id"),...
6
5952
by: Samuel M. Smith | last post by:
I have been playing around with a subclass of dict wrt a recipe for setting dict items using attribute syntax. The dict class has some read only attributes that generate an exception if I try to assign a value to them. I wanted to trap for this exception in a subclass using super but it doesn't happen. I have read Guido's tutorial on new...
3
4062
by: Rahul Anand | last post by:
As per our requirements we have a web service which internally connects (Simple HTTP Post Request) to a remote server to initiate some work. We are calling the web service method asynchronously from a .NET Web Application hosted on IIS. In our setup the web request form a client can be running for long duration (may be more than 4 hours). ...
2
2059
by: Dimitri Furman | last post by:
SQL Server 2000 SP4. Running the script below prints 'Unexpected': ----------------------------- DECLARE @String AS varchar(1) SELECT @String = 'z' IF @String LIKE ''
0
7675
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. ...
1
7440
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...
0
5997
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5344
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...
0
4963
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
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...
0
3451
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1902
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
726
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...

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.