Python Forum

Full Version: now i wish Python had an inline commant
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
You can try this
def comment(*args):
    pass

x = comment(0x75bcd15) or "spam" 

print(x) # prints spam
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.
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)
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).
Then use only multiline output
[
    123456789, #0x75bcd15
    123456789, #0x75bcd15
    {
       123456789 #0x75bcd15
       : "spam" 
}]
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.
Note that
(123456789 or 0x75bcd15)
with parentheses works in a single line context.
(-1 or 0x75bcd15)
Output:
-1
:-p
i like it.

(366 or 'the number of days in a leap year')