Implementing Rust's dbg! in Python
rtpg.co
rtpg.co
In [8]: print(f'{foo.bar[1]=}') foo.bar[1]='2'
You can do more complex expressions:
In [9]: f'{1+1=}' Out[9]: '1+1=2'
It's not quite as fluid as dbg!(), but it's nice.
Apparently it's inspired by Haskell.[0] I'd love to see it officially picked up in other languages.
[0] https://rust-lang.github.io/rfcs/2361-dbg-macro.html#prior-a...
For an example of how this plays out, I had some code:
let x = y + z;
I wasn't sure what y was, so I was able to let x = dbg!(y) + z;
this is much easier to insert into (and remove from) existing code. Plus, you don't need to write the formatting code.Basically, what you posted is in the same genre of thing, but is very different in a lot of meaningful ways.
let c = dbg!(dbg!(a) + dbg!(b));
one time... #include <iostream>
#include <string>
template<typename T>
T print_dbg(T val, std::string exp, std::string file, int line)
{
std::cerr << exp << " = " << val << " @ " << file << ":" << line << "\n";
return val;
}
#define dbg(x) print_dbg(x,#x,__FILE__,__LINE__) double c = dbg(dbg(a) + dbg(b));
gives a = 1 @ debug.cxx:16
b = 2 @ debug.cxx:16
dbg(a) + dbg(b) = 3 @ debug.cxx:16
which has extra dbg()s on the third line.If I could I would probably want to write
double dbg(c) = dbg(a) + dbg(b);
but that of course doesn't work.Ironically my experience with rust is the opposite, i.e. not using println/dbg debugging is most times does wast time. (Except if you do certain thinks with unsafe code).
The difference is I guess that in rust I normally encode much more simple invariant in the type system and as such most times a bug happens I often can pin-down it's rough position by the way it appears (i.e. panics/errors not subtle timing bugs). In turn I just need to get a nice formatted printout of this one or two values. Instead of having to consider all the other ways things might have cone wrong in python which can't happen in rust (through python3's type annotation + linting can help).
And then, sometimes, you need to debug the debugger...
%(filename_lineno)s will give you the filename and line.
not to mention you can run via pdb and display or stop at points of interest.
what am I missing?
>>> foo = {
... 'bar': 3
... }
>>> print(eval("foo['bar'[]"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
foo['bar'[]
^
SyntaxError: unexpected EOF while parsingI think submitters are able to correct the title for a while and that when they do it might(?) override some or all of the rules in the filter. But the time window for doing so has probably been passed now.
Mods could still change the title though.
Either to the original:
Implementing Rust's dbg! in Python
Or if they want to help out those unfamiliar with Rust syntax they might make the title be:
Implementing Rust's dbg! macro in Python
And if they are opposed to use the bang even in this context it would then be fine IMO to either preferably set the title to:
Implementing Rust's dbg macro in Python
Or less preferably to set the title to:
Implementing Rust's debug macro in Python