Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In Python, the rule is that if a line ends with a bracket/brace/parenthesis waiting to be closed, then the newline is read as whitespace and the expression is considered to continue on the next line. This explains the behaviour in the second example.

In the first example, the preparser gets involved, which does not handle newlines in the same way. Indeed, you can get a syntax error by putting the newline in a slightly different position:

sage: Qpart(x) = 5*( unit_step(x)*
....: sin(x) -
....: sin(x))
SyntaxError: invalid syntax

You can see what happens if you look at what the preparser does:

sage: preparse("""Qpart(x) = 5*( unit_step(x)*
....: sin(x) -
....: sin(x))""") 
'__tmp__=var("x"); Qpart = symbolic_expression(Integer(5)*( unit_step(x)*).function(x)\nsin(x) -\nsin(x))'

The rewriting only happens on the first line, regardless of whether that maintains proper syntax: it's a fairly basic regexp replacement. Ideally the preparser would actually PARSE, but that's not what it does.