Apostrophe in docstring?

35 views
Skip to first unread message

kcrisman

unread,
Oct 29, 2008, 10:21:03 PM10/29/08
to sage-support
Hello support,

I came across some very strange behavior recently regarding docstrings
in functions. In the notebook in 3.2.alpha0 and 3.0.6, putting an
apostrophe in the docstring causes various bugs. It doesn't have to
be in any particular spot, as far as I can tell; in fact, it took a
long time to pinpoint this as the source of the problem!

More precisely, it seems to cause the preparser to turn off or
something, because it disallows the following (in the function
definition):
f(x)=x^3
because of the f(x), and
f=x^3
because of the ^ used instead of ** (the error message for the first
is very mysterious at times, but the second one is always clear when
you try to use the function).

I can post a link to a worksheet if it's really necessary, but
hopefully this will be enough for someone to help. I checked Sage and
Python docs about documentation strings, but couldn't find anything
about it being bad to have apostrophes; in fact, some Sage docstrings
have them (e.g. for declaring variables). I didn't try this in the
command line, so it is possible it's only a notebook issue.

Thanks for any help!
- kcrisman
Message has been deleted

kcrisman

unread,
Oct 30, 2008, 9:22:30 AM10/30/08
to sage-support


>
> Yes. Could you please post the said link. I really want to know what
> caused the problem you're describing.

http://math.uchicago.edu/~crisman/Weird_Notebook_Behavior.sws

I would try it out sagenb.org, but... :) However, I did try it on our
departmental server (which is 3.0.4, I think) and it also blew up.

Sample function which causes problems:

sage: def plot_or_not():
....: '''
....: What's up with this? x^3
....: '''
....: f(x)=x^3
------------------------------------------------------------
File "<ipython console>", line 5
SyntaxError: can't assign to function call (<ipython console>, line 5)

Experimentation reveals that the apostrophe is causing the trouble.
As you can see, I now have had time to try this in the console, as
well as the other problems on the linked worksheet, so I'm baffled.

I hope it's not my architecture - PPC OSX.4.

- kcrisman

John Cremona

unread,
Oct 30, 2008, 9:33:34 AM10/30/08
to sage-s...@googlegroups.com
Does it help if you replace the initial ''' with r''' ?

John Cremona

2008/10/30 kcrisman <kcri...@gmail.com>:

Simon King

unread,
Oct 30, 2008, 9:35:54 AM10/30/08
to sage-support
Dear Kcrisman,

On Oct 30, 2:22 pm, kcrisman <kcris...@gmail.com> wrote:
> sage: def plot_or_not():
> ....: '''
> ....: What's up with this? x^3
> ....: '''
> ....: f(x)=x^3
> ------------------------------------------------------------
> File "<ipython console>", line 5
> SyntaxError: can't assign to function call (<ipython console>, line 5)

I get the same (console, Suse linux, x86_64).

It works fine if you use triple-" and not triple-' :
sage: def plot_or_not():
....: """
....: What's up with this?
Object `What's up with this` not found.
....: """
....: f(x)=x^3
....:
sage: plot_or_not()

The complaint "Object `What's up with this` not found." is disturbing,
but eventually sage swallows the definition.

It *should* work with triple-' as well, I believe. But at least
triple-" is a work-around.

Cheers
Simon

Simon King

unread,
Oct 30, 2008, 9:40:53 AM10/30/08
to sage-support
Dear John,

On Oct 30, 2:33 pm, "John Cremona" <john.crem...@gmail.com> wrote:
> Does it help if you replace the initial ''' with r''' ?

Unfortunately:

sage: def plot_or_not():
....: r'''
....: What's up with this?
Object `What's up with this` not found.
....: '''
....: f(x)=x^3
------------------------------------------------------------
File "<ipython console>", line 5
SyntaxError: can't assign to function call (<ipython console>, line 5)


Cheers
Simon

kcrisman

unread,
Oct 30, 2008, 9:59:54 AM10/30/08
to sage-support

>
> It *should* work with triple-' as well, I believe. But at least
> triple-" is a work-around.

Simon,

I didn't try that, because my own workaround was just to delete the
docstring completely. It was a lab on Newton's method for students,
and to be honest they don't really need the docstring. But I
appreciate this workaround too for future reference - thanks!

Interestingly, if I had called it the Newton-Raphson method I would
have never discovered this.

- kcrisman

Jason Grout

unread,
Oct 30, 2008, 10:23:23 AM10/30/08
to sage-s...@googlegroups.com


I think the problem in the above is the question mark, not the
apostrophe. Removing the question mark makes the error go away for me.

Jason

Justin C. Walker

unread,
Oct 30, 2008, 10:49:01 AM10/30/08
to sage-s...@googlegroups.com

On Oct 30, 2008, at 06:22 , kcrisman wrote:
>> Yes. Could you please post the said link. I really want to know what
>> caused the problem you're describing.
>
> http://math.uchicago.edu/~crisman/Weird_Notebook_Behavior.sws
>
> I would try it out sagenb.org, but... :) However, I did try it on our
> departmental server (which is 3.0.4, I think) and it also blew up.
>
> Sample function which causes problems:
>
> sage: def plot_or_not():
> ....: '''
> ....: What's up with this? x^3
> ....: '''
> ....: f(x)=x^3
> ------------------------------------------------------------
> File "<ipython console>", line 5
> SyntaxError: can't assign to function call (<ipython console>, line 5)

Coming in late, but if this is literally what your example is, I think
the problem is the "f(x)=x^3". Can you do that?

If I try this

def foo(x):
f(x) = x^3
return f(x)

on 3.2.alpha0, without any doctstring, it blows up with

TypeError: Must construct a function with a tuple (or list) of
SymbolicVariables.

when I try to run it. If I add a docstring with """, it blows up the
same way. If I add a docstring with ''', it blows up with the syntax
error. Likewise, if I try this with "sage -python", I get the syntax
error.

Also, I tried this:
def foo(x):
x = x^2
return x

If I put in a docstring with """, foo(3) returns 9; with ''', foo(3)
returns 1 !

It seems that the ''' docstring causes the definition to bypass Sage,
while with the """ docstring, the definition is handled by Sage.

Is that possible?

Justin

--
Justin C. Walker, Curmudgeon at Large
Institute for the Absorption of Federal Funds
-----------
Like the ski resort full of girls hunting for husbands
and husbands hunting for girls, the situation is not
as symmetrical as it might seem.
- Alan MacKay
--

Justin C. Walker

unread,
Oct 30, 2008, 10:55:29 AM10/30/08
to sage-s...@googlegroups.com
Rats...

I meant to say in the discussion below that if your docstring is like
'''
That's all folks
...

you see the "bypass Sage" behavior, while the same string with """
gives you expected behavior.

So perhaps two problems:
- parser gets confused with too many single quotes (Sage parser
problem)
- can't assign to a function (language definition problem)

Does Sage permit "f(x) = x^3"? Python doesn't, I think.

Justin

If it weren't for carbon-14, I wouldn't date at all.
-----------


kcrisman

unread,
Oct 30, 2008, 10:57:13 AM10/30/08
to sage-support

>
> I think the problem in the above is the question mark, not the
> apostrophe.  Removing the question mark makes the error go away for me.

I'm not saying the question mark might not be a problem too; it
doesn't yield any unusual behavior for me, but I may not have tested
it enough. But any time I put the apostrophe in, I get problems. In
the link to the worksheet posted, behavior like this resulted, which
is unrelated to the question mark.

sage: def plot_or_not():
....: '''
....: ' x^3
....: '''
....: f(x)=x^3
------------------------------------------------------------
File "<ipython console>", line 5
SyntaxError: can't assign to function call (<ipython console>, line 5)

sage: def plot_or_not():
'''
' x^3
'''
f=x^3
plot(f)
....:
sage: plot_or_not()
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call
last)
<snip>
.../element.pyx in sage.structure.element.Element.__xor__ (sage/
structure/element.c:3382)()

RuntimeError: Use ** for exponentiation, not '^', which means xor
in Python, and has the wrong precedence.

kcrisman

unread,
Oct 30, 2008, 11:11:50 AM10/30/08
to sage-support


> Coming in late, but if this is literally what your example is, I think  
> the problem is the "f(x)=x^3".  Can you do that?
>
> If I try this
>
> def foo(x):
>      f(x) = x^3
>      return f(x)
>
> on 3.2.alpha0, without any doctstring, it blows up with


Thanks for your thoughts. Interesting, because I don't have any
problems with that! In fact, I think your problem there is that you
have x as both the input and the variable in your definition. E.g.,
your example blows up for me too, but I have no problems with the
following:

sage: def new():
....: f(x)=x^3
....: return f(3)
....:
sage: new()
27
sage: def new1():
f(x)=x^3
return f(x)
....:
sage: new1()
x^3

It sounds like there are several interesting things developing out of
this thread!

- kcrisman

William Stein

unread,
Oct 30, 2008, 11:17:22 AM10/30/08
to sage-s...@googlegroups.com
On Thu, Oct 30, 2008 at 6:22 AM, kcrisman <kcri...@gmail.com> wrote:
>
>
>
>>
>> Yes. Could you please post the said link. I really want to know what
>> caused the problem you're describing.
>
> http://math.uchicago.edu/~crisman/Weird_Notebook_Behavior.sws
>
> I would try it out sagenb.org, but... :)

For the record, sagenb.org is nearly back. I configured a virtual
machine, put all the sagenb.org worksheets in it, got it a static
ip address on the network, etc. Everything worked locally, but
there was some nontrivial problem probably with the university's
local firewall or other networking configuration, which I still have
to resolve. So with luck sagenb.org will be back very soon.

Also, having it run in its own dedicated isolated virtual machine is
very nice, since I can tell exactly how much resources it is using
at any given time, which will be very helpful for debugging.

> However, I did try it on our
> departmental server (which is 3.0.4, I think) and it also blew up.
>
> Sample function which causes problems:
>
> sage: def plot_or_not():
> ....: '''
> ....: What's up with this? x^3
> ....: '''
> ....: f(x)=x^3
> ------------------------------------------------------------
> File "<ipython console>", line 5
> SyntaxError: can't assign to function call (<ipython console>, line 5)
>
> Experimentation reveals that the apostrophe is causing the trouble.
> As you can see, I now have had time to try this in the console, as
> well as the other problems on the linked worksheet, so I'm baffled.
>
> I hope it's not my architecture - PPC OSX.4.
>
> - kcrisman
> >
>

--
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org

Justin C. Walker

unread,
Oct 30, 2008, 11:20:02 AM10/30/08
to sage-s...@googlegroups.com

On Oct 30, 2008, at 08:11 , kcrisman wrote:

>
>
>
>> Coming in late, but if this is literally what your example is, I
>> think
>> the problem is the "f(x)=x^3". Can you do that?
>>
>> If I try this
>>
>> def foo(x):
>> f(x) = x^3
>> return f(x)
>>
>> on 3.2.alpha0, without any doctstring, it blows up with
>
>
> Thanks for your thoughts. Interesting, because I don't have any
> problems with that! In fact, I think your problem there is that you
> have x as both the input and the variable in your definition. E.g.,
> your example blows up for me too, but I have no problems with the
> following:

[snip]


> It sounds like there are several interesting things developing out of
> this thread!

Yup. I was a bit too focused on the quote mark issues :-}

Removing the quotes, as well as the reuse of formal variables :-},
from the picture makes things a bit clearer.

It does look like there's a confusion arising in the parser when too
many quotes intrude...FWIW, if I use two 's in the doc string, things
work correctly (or, at least, as expected) :-}

Justin

--
Justin C. Walker, Curmudgeon-At-Large


Institute for the Absorption of Federal Funds
--------

Men are from Earth.
Women are from Earth.
Deal with it.
--------

Message has been deleted

William Stein

unread,
Oct 30, 2008, 3:10:08 PM10/30/08
to sage-s...@googlegroups.com
On Thu, Oct 30, 2008 at 12:02 PM, Minh Nguyen <nguye...@gmail.com> wrote:
>
> Hi kcrisman,
> OK, here's my experimentation from within the console sage-3.1.4, x86
> machine, Debian 5.0 Lenny (testing):
>
> Yep, I received a similar error as you described:
>
> sage: def foo():

> ....: '''
> ....: What's up with this? x^3
> ....: '''
> ....: f(x) = x^3
> ------------------------------------------------------------
> File "<ipython console>", line 5
> SyntaxError: can't assign to function call (<ipython console>, line 5)
>
> John's suggestion about replacing the ''' with r''' results in the same error:
>
> sage: def foo():
> ....: r'''

> ....: What's up with this? x^3
> ....: '''
> ....: f(x) = x^3
> ------------------------------------------------------------
> File "<ipython console>", line 5
> SyntaxError: can't assign to function call (<ipython console>, line 5)
>
> As Simon has said, the error seems to go away when I used triple "
> (double quotation mark, not single quotation mark), which I normally
> do anyway whenever I write docstring. But using triple ''' with or
> without the leading r, and with or without the question mark, resulted
> in a siimilar error:
>
> sage: def foo():

> ....: '''
> ....: What's up with this? x^3
> ....: '''
> ....: f(x) = x^3
> ------------------------------------------------------------
> File "<ipython console>", line 5
> SyntaxError: can't assign to function call (<ipython console>, line 5)
>
> sage: def foo():
> ....: r'''
> ....: What's up here x^3
> ....: '''
> ....: f(x) = x^3

> ------------------------------------------------------------
> File "<ipython console>", line 5
> SyntaxError: can't assign to function call (<ipython console>, line 5)
>
> --
> Regards
> Minh Van Nguyen

Can you make a trac ticket with the above info? It should be enough to
make it "easy" for somebody to fix the problem, which is surely a bug
in the Sage preparser (and is surely my fault).

-- William

Reply all
Reply to author
Forward
Message has been deleted
0 new messages