Python Forum
can a number preceed a line of code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can a number preceed a line of code?
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
(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
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
(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.
« We can solve any problem by introducing an extra level of indirection »
Reply
#5
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  code style - one line subclause on same line Skaperen 5 3,740 Nov-29-2017, 01:44 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020