Inplace operations on Data object copies

Asked by Lutz Gross

Why does the script

  from esys.escript import *
  from esys.finley import Rectangle
  d=Rectangle()
  x=Scalar(1.,ContinuousFunction(d))
  y=x
  x+=1.
  print "x= ",x," y= ", y

return

  x = 2 and y = 2

and not x=2 and y=1 ?

Posted on behalf of Louise Olson-Kettle

Question information

Language:
English Edit question
Status:
Answered
For:
esys-escript Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Lutz Gross (l-gross) said :
#1

The statement y=x creates a shallow copy only. That means that the underlying data are not copied. Therefore an update of x will effect y. To make a deep copy you can use the copy method (which needs some revision) or - which is easier at this stage - use the statement y=x*1..

Revision history for this message
Joel Fenwick (j-fenwick1) said :
#2

It is better to use the copy method now.

so to make y an independent copy of x use:

y=x.copy()

Can you help with this problem?

Provide an answer of your own, or ask Lutz Gross for more information if necessary.

To post a message you must log in.