From: candide on
The escape sequence \ENTER allows to split a string over 2 consecutive
lines. On the other hand, it seems impossible to split a numeric
litteral across multiple lines, compare :

>>> "1000\
.... 000\
.... 000"
'1000000000'
>>> 1000\

.... 000\
File "<stdin>", line 2

000\
^

SyntaxError: invalid syntax
>>>


Is this the general behaviour ? So, how do you edit code containing a
very very long numeric constant ?
From: Steven D'Aprano on
On Thu, 15 Jul 2010 03:30:24 +0200, candide wrote:

> The escape sequence \ENTER allows to split a string over 2 consecutive
> lines. On the other hand, it seems impossible to split a numeric
> litteral across multiple lines
[...]
> Is this the general behaviour ?

Yes. You can't put any whitespace in the middle of a numeric literal:


>>> n = 4 2
File "<stdin>", line 1
n = 4 2
^
SyntaxError: invalid syntax



> So, how do you edit code containing a very very long numeric constant ?



s = (
"1234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890"
)
assert len(s) == 200
n = int(s)



--
Steven
From: MRAB on
candide wrote:
> The escape sequence \ENTER allows to split a string over 2 consecutive
> lines. On the other hand, it seems impossible to split a numeric
> litteral across multiple lines, compare :
>
> >>> "1000\
> ... 000\
> ... 000"
> '1000000000'
> >>> 1000\
> ... 000\
> File "<stdin>", line 2
> 000\
> ^
> SyntaxError: invalid syntax
> >>>
>
>
> Is this the general behaviour ? So, how do you edit code containing a
> very very long numeric constant ?

Normally it's only string literals that could be so long that you might
want to split them over several lines. It is somewhat unusual to have a
_numeric_ literal that's very very long!

For an integer literal you could use a string literal and convert it to
an integer:

>>> int("1000\
000\
000")
1000000000
>>>
From: candide on
MRAB a �crit :

> want to split them over several lines. It is somewhat unusual to have a
> _numeric_ literal that's very very long!
>

I agree. But consider RSA-155 for instance ... ;)


> For an integer literal you could use a string literal and convert it to
> an integer:
>
> >>> int("1000\
> 000\
> 000")
> 1000000000
> >>>

OK.

In C, the following code is allowed :

int x=1000\
000\
000;

but not very usefull for sure !
From: Lawrence D'Oliveiro on
In message <mailman.749.1279159335.1673.python-list(a)python.org>, MRAB wrote:

> Normally it's only string literals that could be so long that you might
> want to split them over several lines. It is somewhat unusual to have a
> _numeric_ literal that's very very long!

Seems a peculiar assumption to make in a language that allows integers of
arbitrary length, does it not?