Kevin,

I have been taking the same plunge (and prbably for the same reason).
It's not clear whether knowing C++ well was a help or a hindrance.  I
have just read the book "Learning Python" by Lutz, published by
O'Reilly, and found it pretty good.  At least, I knew immediately why
your progam did what it did and how to avoid it as there's an almost
identical example in the book!

Now after "Learning Python" at 700pp I can move on to "Programming
Python" -- same author and publisher but 1500 pages.  Or maybe not....

John

On 15/01/2008, William Stein <[EMAIL PROTECTED]> wrote:
>
> Kevin Buzzard wrote:
> > I finally took the plunge and started learning python. I think it's the
> > first language I've ever learnt where "everything is a pointer" and it was
> > a big psychological shock! After my first day of messing around with it, I
> > was utterly confused :-) I had a vector which I wanted to modify several
> > times, and I wanted to keep track of what the vector looked like at
> > various stages, so I initiated a list and then wrote a loop which, on
> > every iteration, modified the vector and then appended the modified vector
> > to the end of the list. Of course, when the loop had ended, the list
> > contained 100 copies of the final state of the vector. I had no idea what
> > to do! I asked David Loeffler and he told me about loading various modules
> > and using functions like deepcopy() and it all left me feeling wholly
> > bewildered! On the other hand I am kind of coming around to the idea now.
>
> Here's the sort of thing you might want to do:
>
> sage: v = vector(ZZ, 5)
> sage: w = [v]
> sage: for i in range(10):
> ....:     z = copy(w[-1]); z[0] += 1; w.append(z)
> ....:
> sage: w
> [(0, 0, 0, 0, 0),
>  (1, 0, 0, 0, 0),
>  (2, 0, 0, 0, 0),
>  (3, 0, 0, 0, 0),
>  (4, 0, 0, 0, 0),
>  (5, 0, 0, 0, 0),
>  (6, 0, 0, 0, 0),
>  (7, 0, 0, 0, 0),
>  (8, 0, 0, 0, 0),
>  (9, 0, 0, 0, 0),
>  (10, 0, 0, 0, 0)]
>
>
> This is indeed totally different than Magma which uses call by value
> semantics.  Python is much closer semantically to C/C++, where most serious
> programs pass around pointers.
>
> In Magma:
>
> > v := VectorSpace(RationalField(), 5)!0;
> > w := [v,v,v];
> > v[1] := 5;
> > w;
> [
>     (0 0 0 0 0),
>     (0 0 0 0 0),
>     (0 0 0 0 0)
> ]
>
> In Sage/Python:
>
> sage: v = vector(ZZ, 5)
> sage: w = [v,v,v]
> sage: v[1] = 5
> sage: w
> [(0, 5, 0, 0, 0), (0, 5, 0, 0, 0), (0, 5, 0, 0, 0)]
>
> In Python w = [v,v,v] makes a list with 3 references to a single object.
> In Magma it *tends* to make a list of 3 copies, if copying of objects
> happens to be defined -- otherwise magma *still* makes a list of
> 3 references !!
>
> > v := ModularSymbols(389);
> > w := [v,v,v];
> > v`dimension := 10;
> > w[2]`dimension;
> 10
>
> So Magma is just plain inconsistent, which in the long run is
> more confusing than Python.  This is especially relevant when
> it comes to complicated data structures (common in mathematics)
> where copying is just not defined or is very expensive.
>
>
> By the way the copy Python function is usually enough to copy
> most Sage objects sufficiently for most purposes.   Deep copy
> is needed only in certain special cases, e.g., a list of lists...
>
> --
> William Stein
> Associate Professor of Mathematics
> University of Washington
> http://wstein.org
>
> >
>


-- 
John Cremona

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~----------~----~----~----~------~----~------~--~---

Reply via email to