469,643 Members | 1,282 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,643 developers. It's quick & easy.

working with pointers

Do expicit pointers exist in python??

if i do:

a = [5,7]
b = a

a.empty()

b = ?

how do i do explicit pointers??

Mike
Jul 19 '05 #1
15 1404
Michael wrote:
Do expicit pointers exist in python??

if i do:

a = [5,7]
b = a

a.empty()

b = ?
This is what the interactive prompt is for. Try it:

py> a = [5,7]
py> b = a
py> a.empty()
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'list' object has no attribute 'empty'

Well, looks like you get an AttributeError. Let's try a method that
actually exists instead:

py> a.pop()
7
py> a
[5]
py> b
[5]

So, as you can see, since 'a' and 'b' are both names referring to the
same object, when you modify the object referred to by 'a', you are also
modifying the object referred to by 'b'.
how do i do explicit pointers??


I don't know what you mean by "explicit pointers". Care to elaborate?
It also might help if you explained what it is you think you want
"explicit pointers" to do.

STeVe
Jul 19 '05 #2
"Steven Bethard" <st************@gmail.com> wrote in message
news:JJ********************@comcast.com...
Michael wrote:
Do expicit pointers exist in python??

if i do:

a = [5,7]
b = a

a.empty()

b = ?


This is what the interactive prompt is for. Try it:

py> a = [5,7]
py> b = a
py> a.empty()
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'list' object has no attribute 'empty'

Well, looks like you get an AttributeError. Let's try a method that
actually exists instead:

py> a.pop()
7
py> a
[5]
py> b
[5]

So, as you can see, since 'a' and 'b' are both names referring to the
same object, when you modify the object referred to by 'a', you are also
modifying the object referred to by 'b'.
how do i do explicit pointers??


I don't know what you mean by "explicit pointers". Care to elaborate?
It also might help if you explained what it is you think you want
"explicit pointers" to do.

STeVe


sorry, I'm used to working in c++ :-p

if i do
a=2
b=a
b=0
then a is still 2!?

so when do = mean a reference to the same object and when does it mean make
a copy of the object??

regards

Mike
Jul 19 '05 #3
Michael wrote:
sorry, I'm used to working in c++ :-p

if i do
a=2
b=a
b=0
then a is still 2!?

so when do = mean a reference to the same object
Always.
and when does it mean make a copy of the object??


Never.

-Dave
Jul 19 '05 #4
except numbers??

"Dave Brueck" <da**@pythonapocrypha.com> wrote in message
news:ma**************************************@pyth on.org...
Michael wrote:
sorry, I'm used to working in c++ :-p

if i do
a=2
b=a
b=0
then a is still 2!?

so when do = mean a reference to the same object


Always.
and when does it mean make a copy of the object??


Never.

-Dave

Jul 19 '05 #5
Hi All--

Dave Brueck wrote:

Michael wrote:
sorry, I'm used to working in c++ :-p

if i do
a=2
b=a
b=0
then a is still 2!?

so when do = mean a reference to the same object


Always.
and when does it mean make a copy of the object??


Never.


To which I would add (without attempting to preserve Dave's admirable
brevity):

a=[3,5,6]
b=a

"b" is a reference to a; both b and a are names bound to "[3,5,6]".

a=[3,5,6]
b=a[:]

a and b are now bound to different instances of [3,5,6]

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
Jul 19 '05 #6
Michael wrote:
a=2
b=a
b=0

That's more or less equivalent to this C++ code:

int *a;
int *b;
a = new int;
*a = 2;
b = a;
b = new int;
*b = 0;
Jul 19 '05 #7
Michael wrote:
if i do
a=2
b=a
b=0
then a is still 2!?

so when do = mean a reference to the same object and when does it mean make
a copy of the object??


It *always* means a reference. It *never* makes a copy.

Although the terminology isn't quite right, you can think of all
"variables" in Python being "references". Assignment statements in
Python then simply change the object that a "variable" "points" to.

Your example with integers:

py> a = 2
py> b = a
py> b = 0
py> a
2
py> b
0

A simlar example with lists:

py> a = [5, 7]
py> b = a
py> b = []
py> a
[5, 7]
py> b
[]

Of course, if you modify an object while two names are bound to it ("two
variables hold pointers to it") then the modifications will be visible
through either name ("either pointer"):

py> a = [5, 7]
py> b = a
py> a.pop()
7
py> a
[5]
py> b
[5]

Note that since integers are immutable, I can't give you a direct
example like this with integers, but try:

py> class I(int):
.... pass
....
py> a = I(42)
py> a
42
py> b = a
py> b
42
py> a.flag = True
py> b.flag
True
py> a.flag = False
py> b.flag
False

So even with ints (or at least a mutable subclass of ints),
modifications made to an object through one name ("reference") are also
visible through other names ("references") to that object.

HTH,

STeVe
Jul 19 '05 #8
On 2005-05-31, Michael <sl***********@hotmail.com> wrote:
except numbers??


Um, no?

Unless you provide some context, how are we supposed to know
what you're asking about?

Numbers are immutable, so there is no practical difference.

--
Grant Edwards grante Yow! Are we THERE yet? My
at MIND is a SUBMARINE!!
visi.com
Jul 19 '05 #9
> "Dave Brueck" <da**@pythonapocrypha.com> wrote in message
news:ma**************************************@pyth on.org...
Michael wrote:
sorry, I'm used to working in c++ :-p

if i do
a=2
b=a
b=0
then a is still 2!?

so when do = mean a reference to the same object
Always.

and when does it mean make a copy of the object??


Never.


Michael wrote: except numbers??


1) Please avoid top posting

2) '=' always makes a reference. It's just that

'b = 0' makes a *new* reference for b to 0, without changing the
reference of a to 2.

b.pop() modifies the object referenced by b itself, which, since it is
referenced by both a & b, means that the object referenced by a is also
modified.

see also:

http://starship.python.net/crew/mwh/...jectthink.html
Jul 19 '05 #10
Michael wrote:
sorry, I'm used to working in c++ :-p

if i do
a=2
b=a
b=0
then a is still 2!?

so when do = mean a reference to the same object and when does it mean make
a copy of the object??


To understand this in C++ terms, you have to treat everything, including
simple integers, as a class instance, and every variable is a reference
(or "smart pointer".) The literals '0' and '2' produce integer class
instances rather than primitive integers. Here's a reasonable C++
translation of that code, omitting destruction issues:

class Integer
{
private:
int value;
public:
Integer(int v) { value = v; }
int asInt() { return value; }
}

void test()
{
Integer *a, *b;
a = new Integer(2);
b = a;
b = new Integer(0);
}

In that light, do you see why a is still 2?

Shane
Jul 19 '05 #11
On Tue, 31 May 2005 13:13:00 -0600, Steven Bethard
<st************@gmail.com> declaimed the following in comp.lang.python:

Although the terminology isn't quite right, you can think of all
"variables" in Python being "references". Assignment statements in
Python then simply change the object that a "variable" "points" to.
Not the clearest, as "change the object" sounds like an in place
modification. Assignment statements, in my humble vague terms, change
the /variable/ to /point/ to a different object

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 19 '05 #12
Shane Hathaway wrote:
Michael wrote:
sorry, I'm used to working in c++ :-p

if i do
a=2
b=a
b=0
then a is still 2!?

so when do = mean a reference to the same object and when does it
mean make a copy of the object??


To understand this in C++ terms, you have to treat everything,
including simple integers, as a class instance, and every variable is
a reference (or "smart pointer".) The literals '0' and '2' produce
integer class instances rather than primitive integers. Here's a
reasonable C++ translation of that code, omitting destruction issues:

class Integer
{
private:
int value;
public:
Integer(int v) { value = v; }
int asInt() { return value; }
}

void test()
{
Integer *a, *b;
a = new Integer(2);
b = a;
b = new Integer(0);
}

A closer translation would be:

const Integer CONST0(0);
const Integer CONST2(2);

void test()
{
const Integer *a, *b;
a = &CONST0;
b = a;
b = &CONST2;
}

The constant integers are created in advance, not when you do the
assignment. Arithmetic may create new Integer objects, but when the result
is a small integer it simply reuses an existing object.
Jul 19 '05 #14
Duncan Booth wrote:
The constant integers are created in advance, not when you do the
assignment.


But that's just an optimization, not Python's defined behavior. It seems
more useful to me to think of all integers as being created at
assignment time, even if CPython doesn't actually do that.
Jul 19 '05 #15
Leif K-Brooks wrote:
Duncan Booth wrote:
The constant integers are created in advance, not when you do the
assignment.


But that's just an optimization, not Python's defined behavior. It seems
more useful to me to think of all integers as being created at
assignment time, even if CPython doesn't actually do that.


Alternatively think of all integers as existing all of the time, whether or
not they are referenced.

Yes, it is just an optimisation, but it is one which results in an
observable difference in behaviour. i.e. if all integers are created as
they are needed you will never share integers.

Of course, if all integers existed all of the time then I guess you might
expect 'a==b' to always imply 'a is b', so my interpretation is differently
inaccurate.
Jul 19 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by mufasa | last post: by
4 posts views Thread by knapak | last post: by
6 posts views Thread by Michael Trausch | last post: by
8 posts views Thread by jimmy | last post: by
5 posts views Thread by =?ISO-8859-1?Q?Jonathan_Gro=DF?= | last post: by
25 posts views Thread by J Caesar | last post: by
31 posts views Thread by Markus Pitha | last post: by
4 posts views Thread by Vajkay.Rene | last post: by
2 posts views Thread by Philluminati | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.