Python Forum
now i wish Python had an inline commant - 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: now i wish Python had an inline commant (/thread-19655.html)



now i wish Python had an inline commant - Skaperen - Jul-09-2019

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?


RE: now i wish Python had an inline commant - Gribouillis - Jul-09-2019

You can try this
def comment(*args):
    pass

x = comment(0x75bcd15) or "spam" 

print(x) # prints spam



RE: now i wish Python had an inline commant - Skaperen - Jul-09-2019

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.


RE: now i wish Python had an inline commant - Gribouillis - Jul-10-2019

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)



RE: now i wish Python had an inline commant - Skaperen - Jul-10-2019

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).


RE: now i wish Python had an inline commant - Gribouillis - Jul-10-2019

Then use only multiline output
[
    123456789, #0x75bcd15
    123456789, #0x75bcd15
    {
       123456789 #0x75bcd15
       : "spam" 
}]



RE: now i wish Python had an inline commant - Skaperen - Jul-10-2019

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.


RE: now i wish Python had an inline commant - Gribouillis - Jul-10-2019

Note that
(123456789 or 0x75bcd15)
with parentheses works in a single line context.


RE: now i wish Python had an inline commant - DeaD_EyE - Jul-10-2019

(-1 or 0x75bcd15)
Output:
-1
:-p


RE: now i wish Python had an inline commant - Skaperen - Jul-16-2019

i like it.

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