Re: working with pointers
this might help.. http://effbot.org/zone/python-objects.htm -- http://mail.python.org/mailman/listinfo/python-list
Re: working with pointers
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: working with pointers
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: working with pointers
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. -- http://mail.python.org/mailman/listinfo/python-list
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: working with pointers
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: working with pointers
Steven Bethard [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: working with pointers
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: working with pointers
except numbers?? Dave Brueck [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: working with pointers
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/workshops/1998-11/proceedings.html Army Signal Corps: Cu Chi, Class of '70 Author: Teach Yourself Python in 24 Hours -- http://mail.python.org/mailman/listinfo/python-list
Re: working with pointers
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; -- http://mail.python.org/mailman/listinfo/python-list
Re: working with pointers
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: working with pointers
Nope, numbers too. When you do: a = 4 You are storing a reference to the literal 4 in a. a = 4 dir(a)['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__getattribute__', '__getne wargs__', '__hash__', '__hex__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__redu ce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__'] Unlike a list, there is no way to alter the interger value of 4. If we go further, and then do something like: b = a b4 Both a and b refer to the same object, in this case the 4 object. If you want a copy of the object check out the copy module. Chris On 31/05/05, Michael [EMAIL PROTECTED] wrote: except numbers??Dave Brueck [EMAIL PROTECTED] wrote in messagenews:[EMAIL PROTECTED] 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--http://mail.python.org/mailman/listinfo/python-list-- I was born not knowing and have had only a little time to change that here and there. -- Richard Feynman -- http://mail.python.org/mailman/listinfo/python-list
Re: working with pointers
On 2005-05-31, Michael [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: working with pointers
Dave Brueck [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] 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/hacks/objectthink.html -- http://mail.python.org/mailman/listinfo/python-list
Re: working with pointers
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 -- http://mail.python.org/mailman/listinfo/python-list