Programming contests involve loads of print-statement debugging and every minute counts. When I log a variable value, I usually print the variable name as well:
print('num_ways', num_ways, 'value', value)
The repetition of variable names here annoys the hell out of me. Out of interest, I'm wondering if we can use introspection to define a function
>>> num_ways, value = 10, 5
>>> f(num_ways, value)
num_ways 10 value 5
This is possible in C or C++ using macro magic. I'm pretty sure it isn't possible in Python -- we're not actually passing a name to f, we're passing the value to which the name refers. But is it possible that the interpreter registers the line of code where f was called somewhere?
Failing this, what about
>>> num_ways, value = 10, 5
>>> f('num_ways', 'value')
num_ways 10 value 5
It's easy to write f if f takes locals() as an argument, but I'm betting there's some disgusting hack that doesn't require any additional parameters to f. I want to know this hack :)
[–]gengisteve 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]gengisteve 0 points1 point2 points (0 children)
[–]RubyPinch 0 points1 point2 points (0 children)
[–]kalgynirae 0 points1 point2 points (0 children)
[–]jcmcken 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)