working with pointers

M

Michael

Do expicit pointers exist in python??

if i do:

a = [5,7]
b = a

a.empty()

b = ?

how do i do explicit pointers??

Mike
 
S

Steven Bethard

Michael said:
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
 
M

Michael

Steven Bethard said:
Michael said:
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
 
D

Dave Brueck

Michael said:
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
 
I

Ivan Van Laningham

Hi All--

Dave said:

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/workshops/1998-11/proceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
 
S

Steven Bethard

Michael said:
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
 
G

Grant Edwards

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.
 
S

Shane Hathaway

Michael said:
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
 
D

Dennis Lee Bieber

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

--
 
D

Duncan Booth

Shane said:
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.
 
L

Leif K-Brooks

Duncan said:
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.
 
D

Duncan Booth

Leif said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,240
Messages
2,571,211
Members
47,847
Latest member
ShavonneLa

Latest Threads

Top