On Sep 20, 12:36 am, Stan Schymanski <schym...@gmail.com> wrote:
> Since this is done in the units package already, is there a way to
> formally save the units of e.g. T_a as units.temperature.Kelvin and then
> have something like:
> sage: T_a = 300
> sage: T_a
> 300 Kelvin

You can of course write T_a=300*units.temperature.kelvin and
essentially get this, see the doc.

Otherwise: python semantics get in the way. What you propose would
require T_a to be statically typed to be a temperature, but that is
not how python works. The instruction "T_a=300" rebinds the identifier
T_a to the object 300 (which, thanks to sage's preparser, really is
Integer(300)). Once you execute a new assignment, T_a is bound to a
fresh object and all type info hangs off that object, not T_a.

T_a=300*units.temperature.kelvin
T_a=10*units.length.meter

If you want a separate type that denotes temperatures in kelvin, you
could define the appropriate (sub)class and get something like:

sage: T_a=temperature_in_kelvin(300) #just special printing
sage: T_a
300 Kelvin

or

sage: T_a=temperature_in_kelvin(300) #construct unit expression
sage: T_a
300*kelvin

but that probably has virtually no benefits for you above what you can
already do by just multiplying with the right unit.

[Thank you, William, for pointing out that units already subclass
symbolic expressions to set docs]
[apologies the empty reply before]

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to