On Tue, Jan 12, 2010 at 7:14 PM, Oscar Gerardo Lazo Arjona
<algebraicame...@gmail.com> wrote:
> wouldn't it be a good idea to hard-code certain mathematical expressions
> into sage
> like pi, I , and e as sage additional keywords so that they could not be
> variable names?

Yes.

> I had a hard time figuring out what was happening when my e^x expression
> didn't work
> because i had made e into a string what do you think?

The Python programming language doesn't easily support user-defined
reserved expressions.  However, in the Sage command line (IPython),
and in the Sage notebook, as explained in the thread

http://groups.google.com/group/sage-notebook/browse_thread/thread/6f2cd64ad21ff2a7/e96520862a228454?lnk=gst&q=fernando#e96520862a228454

one can *customize* the globals dictionary used in interactive
evaluation of code.   I think one could thus create a custom
dictionary D so if you type

    e = 10

when Sage tries to do D['e'] = 10, the custom dict D would complain
and raise an error.

As a proof of concept, try pasting this into Sage:


class MyDict(dict):
    def __setitem__(self, key, val):
        if key == 'e':
            raise NameError, "name %s is protected"%key
        dict.__setitem__(self, key, val)

Then try this:

sage: G = MyDict(globals())
sage: exec 'x=5; print x' in G
5
sage: G['x']
5
sage: exec 'e=5; print e' in G
Traceback (most recent call last):
...
NameError: name e is protected
sage: G['e']
e

See how in the second case one isn't allowed to change e.

Sage will soon have a similar mode so that undefined vars magically
spring into existence *and* you can call methods using functional
notation, which is what that thread linked to above is about.

I do not think any of this should be on by *default*.       However,
it should be a simple option to turn any/all of this on with an easy
short command.

It would be helpful if somebody made a list of default protected variables.

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

Reply via email to