Python Forum
now i wish Python had an inline commant
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
now i wish Python had an inline commant
#1
in my print_object() function, which has, so far, succeeded in outputting valid Python code, i am wanting to output ints in hexadecimal as an inline comment after the decimal output. an alternative idea is to output it like 123456789+0*0x75bcd15 or 123456789+0x75bcd15*0. your thoughts?
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
You can try this
def comment(*args):
    pass

x = comment(0x75bcd15) or "spam" 

print(x) # prints spam
Reply
#3
my function is printing a large value, possibly over many lines, or on a very long line (depending on caller's end= option). that can be a large tree of lists, tuples, and dictionaries, with leafs of whatever that might be ints. if it is an int, that is when i want to also show the hexadecimal in addition to the decimal, no matter how large the int value is. the intent is that all the output be valid Python3 source code that could be embedded in a source file to be assigned or used in line. the only way to have a function is a lambda as a leaf node. the output is one big structure.

i'm thinking about more options, such as directing the function to return a string or generate the output one unit at a time, and what base to use for ints.
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
A function call is valid python code. You can very well output
Int(123456789, 0x75bcd15)
You can define somewhere else
def Int(x, comment):
    return int(x)
Reply
#5
defining any function "somewhere else" is not a option. this function outputs the value alone, as source. it has to work with its output placed anywhere in the source code it is being inserted in. a future version will get the created source as a string, either one big string of everything, or node by node from a generator (likely only depth-first since that is how source code does it).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
Then use only multiline output
[
    123456789, #0x75bcd15
    123456789, #0x75bcd15
    {
       123456789 #0x75bcd15
       : "spam" 
}]
Reply
#7
that's what i have been doing (multi-line output) up to now. but i have had cases where single line would be very useful. maybe i'll just not do the hexadecimal in the one line case. the one line case is not for human reading, anyway.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
Note that
(123456789 or 0x75bcd15)
with parentheses works in a single line context.
Reply
#9
(-1 or 0x75bcd15)
Output:
-1
:-p
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#10
i like it.

(366 or 'the number of days in a leap year')
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
  what is the chance for an inline python-in-text processor? Skaperen 2 2,252 Dec-25-2018, 06:36 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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