On Tue, 4 Feb 2020 at 10:39, Steven D'Aprano firstname.lastname@example.org wrote:
I've been doing some work with large ints, of well over 100 digits. For example, this number has 131 digits:
P = 29674495668685510550154174642905332730771991799853043350995075531276838753171770199594238596428121188033664754218345562493168782883
Sometimes I'm tempted to write numbers like that as follows:
P = int('29674495668685510550154174642905332730771991' '79985304335099507553127683875317177019959423' '8596428121188033664754218345562493168782883')
which is nicer to read, except for the minor annoyance of the call to int and the string delimiters.
That got me thinking that it might be Nice To Have if we could split long int literals across multiple lines:
P = 29674495668685510550154174642905332730771991\ 79985304335099507553127683875317177019959423\ 8596428121188033664754218345562493168782883
Or if you don't like backslashes, trailing underscores are currently illegal, so we could use them:
P = 29674495668685510550154174642905332730771991_ 79985304335099507553127683875317177019959423_ 8596428121188033664754218345562493168782883
My immediate instinct was underscores so I favour that over backslashes. And I think that if you need to write huge numbers like that then having a better way to break them up is important (you don't use internal underscores at all in your example, which surprises me, as that would be the first thing I'd do).
But I would ask, do you *really* type numbers like that in manually??? I can imagine them being output from another program, or from information in a webpage, that you copy and paste in here, but I'd be more likely to address that with a comment above the definition, saying how to regenerate the number, and that it was copy-and-pasted from that output. If you're copy/pasting, having to reformat is more awkward, rather than less.
Can you share a bit more about why you need to do this? In the abstract, having the ability to split numbers over lines seems harmless and occasionally useful, but conversely it's not at all obvious why anyone would be doing this in real life.