This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Deep-copy expression

+1 vote

Simple Expression copies are shallow, so

from dolfin import *

exp1 = Expression('t + a*a', t=0.0, a=1.0, degree=2)
exp2 = exp1  # only shallow
exp2.t = 3.14
print(exp1.t)

prints 3.14. How to make a deep-copy of an Expression? (Preferably without dragging the degree and such through the entire code base.)

asked Mar 30, 2017 by nschloe FEniCS User (7,120 points)

This question has been asked here .

See also here. Something like this works:

exp2 = Expression(exp1.cppcode, **exp1.user_parameters, degree=2)

This means I'd have to carry around the degree separately, which I'd really like to avoid. Could I perhaps avoid that by using expr.ufl_element()?

Yes, you should be able to use exp1.ufl_element().degree().

...