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?
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
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
On 12 ene, 21:34, William Stein <wst...@gmail.com> wrote:
> 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.
Wow! I was almost sure I would recieve an explanation why that was
impossible/a bad idea. :)
> 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.
That is great!
> 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.
Why not? The point is save trouble to the newbies. It is no big
difference if
instead of telling them "be careful not to use e as a variable" you
tell them
"if you're going to use e as a variable, then use X option"
Yep. And it doesn't use the "exec <...> in G" trick, so it is a lot
more stupid. But I'll rewrite it to use that trick and also work on
the command line in the not-distant future. While I'm at it, I'll
keep this "protected vars" feature request in mind.
WiliAm
Yep. And it doesn't use the "exec <...> in G" trick, so it is a lot
more stupid. But I'll rewrite it to use that trick and also work on
the command line in the not-distant future. While I'm at it, I'll
keep this "protected vars" feature request in mind.
William