sage keywords

9 views
Skip to first unread message

Oscar Gerardo Lazo Arjona

unread,
Jan 12, 2010, 10:14:15 PM1/12/10
to sage-s...@googlegroups.com
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?

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?

William Stein

unread,
Jan 12, 2010, 10:34:48 PM1/12/10
to sage-support, Fernando Perez
On Tue, Jan 12, 2010 at 7:14 PM, Oscar Gerardo Lazo Arjona
<algebra...@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

Message has been deleted

Oscar Lazo

unread,
Jan 12, 2010, 10:57:17 PM1/12/10
to sage-support

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"

William Stein

unread,
Jan 12, 2010, 11:15:26 PM1/12/10
to sage-support
On Tue, Jan 12, 2010 at 7:42 PM, Minh Nguyen <nguye...@gmail.com> wrote:
> On Wed, Jan 13, 2010 at 2:34 PM, William Stein <wst...@gmail.com> wrote:
>
> <SNIP>

>
>> 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.
>
> See ticket #7482 [1], which has been merged in Sage 4.3.1.alpha0. The
> release tour of Sage 4.3.1 [2] (under construction) contains some
> explanation on how to use this new feature for a mode for automatic
> names. Note that this mode currently only works within the Sage
> notebook.
>
> [1] http://trac.sagemath.org/sage_trac/ticket/7482
>
> [2] http://wiki.sagemath.org/ReleaseTours/sage-4.3.1

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

William Stein

unread,
Jan 12, 2010, 11:53:30 PM1/12/10
to sage-support
On Tue, Jan 12, 2010 at 7:42 PM, Minh Nguyen <nguye...@gmail.com> wrote:
> On Wed, Jan 13, 2010 at 2:34 PM, William Stein <wst...@gmail.com> wrote:
>
> <SNIP>
>
>> 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.
>
> See ticket #7482 [1], which has been merged in Sage 4.3.1.alpha0. The
> release tour of Sage 4.3.1 [2] (under construction) contains some
> explanation on how to use this new feature for a mode for automatic
> names. Note that this mode currently only works within the Sage
> notebook.
>
> [1] http://trac.sagemath.org/sage_trac/ticket/7482
>
> [2] http://wiki.sagemath.org/ReleaseTours/sage-4.3.1

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

Reply all
Reply to author
Forward
0 new messages