Python Forum
can a number preceed a line of code? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: can a number preceed a line of code? (/thread-42084.html)



can a number preceed a line of code? - Skaperen - May-07-2024

is there any case where a line of code that begins with a decimal number expressed as a run of digit characters can be valid Python code in Python3? if i have some logic that removes such numbers (only) from the start of each line, would such logic corrupt or damage the Python code it is processing?


RE: can a number preceed a line of code? - Gribouillis - May-07-2024

(May-07-2024, 05:40 AM)Skaperen Wrote: is there any case where a line of code that begins with a decimal number expressed as a run of digit characters can be valid Python code in Python3?
These lines are valid python code if that's what you mean
232444
7663 + 12333
87633 >> x
Does your preprocessor remove these lines?
>>> class A:
...     def __rrshift__(self, n):
...         print('Hello', n)
... 
>>> a = A()
>>> 1876 >> a
Hello 1876



RE: can a number preceed a line of code? - Skaperen - May-07-2024

it looks like i need to use a scheme with something other than just a number, such as maybe a number followed by something such as ":". i want to be sure this containment scheme cannot ever look like valid Python code. for example, an alternate test would be to test if the whole line is valid Python, to decide to remove the number (if valid, leave the line(s) unchanged). what i need is a string scheme such that decimal digits plus something at the start of a line always makes the line distinguishable from valid Python code so it can be decided that the scheme has been prepended to the line.


RE: can a number preceed a line of code? - Gribouillis - May-08-2024

(May-07-2024, 10:17 PM)Skaperen Wrote: what i need is a string scheme such that decimal digits plus something at the start of a line always makes the line distinguishable from valid Python code
It won't be that easy, consider these examples using line continuation or multiline strings
>>> x = \
... 23
>>>
>>> if x == \
... 23:
...     print('hello')
... 
hello
>>> """What a great number
... 23:
... the best number ever!
... """
'What a great number\n23:\nthe best number ever!\n'
Why do you want to do this? It would be easier if every line had such a number. That would remove ambiguity.


RE: can a number preceed a line of code? - Skaperen - May-09-2024

yeah, the multi-line literal will be a huge problem if i don't thoroughly parse all the Python code (including the multi-line literal and/or continuation). the original goal was mixing the Python code into something else. that thought came about from seeing the output of "cat -n". i'll have to totally re-think that concept.