7 ms·
You can do the same thing with Python 3.8+ by using f-strings and just appending "=" to the variable name: >>> print(f"{d['key'][1]=}") d['key'][1]='on
by kissgyorgy 6y ago
You can do the same thing with Python 3.8+ by using f-strings and just appending "=" to the variable name:
>>> print(f"{d['key'][1]=}")
d['key'][1]='one'
- grenoire 6y agoWHAT... Why ain't anybody talking about this? Brilliant!
- JetAlone 6y agoIn this case, it would seem it took a little reinvention in order to let the wheel's discovery be known.
- willyt 6y agoNot used it, but ic seems to be compatible back to python 2.7 so not really comparable. I can't use the builtin thing in my python code because the host software for my plugins is running python 3.6 embedded and only recently upgraded from 3.3 in the latest release so if I want to maintain backwards compatibility I can never use the new python builtin. I expect lots of other people are in the same position.
- JetAlone 6y agoYeah you're right. I hadn't realized it was so new till a little later... Well, it's good that IC has a stronger use case surface area for those who want it.
- wadkar 6y agoIt was “recently” introduced in Python 3.8. See sibling for link to issue tracker
- neumann 6y agoWait. How does this work?
- ninjin 6y agoThank you, I was unaware of this. While I have lost a talking point in favour of Julia – the `@show` macro – I am happy that my old friends over in Python land has access to some better ergonomics and can stop rubbing this in their face. ;) julia> x = 4711 4711 julia> @show x x = 4711 4711
- masklinn 6y agoI don't know what `@show` does exactly, but Python's is only a limited convenience for print, it still not as good as Rust's dbg![0] or TFA's ic, because it does not (and can not) return its parameter unmodified, so you still need to inject an explicit print and possibly execute the expression multiple times. It's convenient, don't get me wrong, but it's not exactly groundsbreaking. Unlike breakpoint[1]. [0] https://doc.rust-lang.org/std/macro.dbg.html https://doc.rust-lang.org/std/macro.dbg.html [1] https://docs.python.org/3/library/functions.html#breakpoint https://docs.python.org/3/library/functions.html#breakpoint it's not groundsbreaking on its own but the ability to very easily hook into it and replace the breakpoint hook by an arbitrary callable? Super useful.
- pletnes 6y agoDo you have any concrete usage examples of that? I’ve only used breakpoint() to stop execution while debugging.
- sammorrowdrums 6y agoIn the docs you can look up breakpoint, it has a lot of features amongst other things you can register a custom handler. I use it in selenium tests so I can either debug on error or just print the error message and continue
- ash 6y agoCould you provide a concrete example? It's still unclear to me why one wants a custom breakpoint() handler.
- est 6y agoYou can also use the breakpoint() introduced in py3.7 via PEP533 https://www.python.org/dev/peps/pep-0553/ https://www.python.org/dev/peps/pep-0553/
- vonwoodson 6y agoBump. This is the most correct way to debug python programs VS using print statement.
- taneq 6y agoDebugging virtually anything real-time that interacts with other systems will require some kind of logging (of which printf() is the trivial case). Step-through debugging is great but far from universally applicable.
- vlovich123 6y agoSetting a breakpoint that prints and continues is appropriate if performance doesn’t matter. It gets trickier if it does. In those cases RR seems like the best idea rather than prints. At least that’s the theory. In my experience the conditions on the ground are different though. Tooling availability is inconsistent, which is why such approaches don’t always work. Reverse debugging isn’t available on all platforms/languages. Debuggers (or at least the C/C++ ones) are slow in how they inject instrumentation code to evaluate - rather than compiling expressions into native codes to conditionally trap, they seem to trap unconditionally and evaluate the conditional using their introspection language (there are valid reasons why it’s done this way but it has serious performance impacts). In compiled languages, the evaluation of the expression can be difficult/impossible to write because of this (not enough type information available, validation is late binding meaning mistakes are extra expensive, etc. At the end of the day, when your tools fail you, print debugging is easier to use to accomplish the task rather. Getting tooling to work effectively is more time consuming and frequently not possible.
- gnulinux 6y agoCorrect in what way? I can think of many real time systems that cannot be debugged with a pdb breakpoint.
- mkl 6y agoYou can also make it a bit prettier by adding spaces: >>> print(f"{d['key'][1] = }") d['key'][1] = 'one' It works with any expression you like, not just variables: >>> print(f'{np.sin(np.pi/4.) = }') np.sin(np.pi/4.) = 0.7071067811865475
- amelius 6y ago> It works with any expression you like Also assignment expressions? :)
- Aeolun 6y agoNot sure if I would consider that an improvement.
- delaaxe 6y agoYes. Please use more of the builtins instead of adding more libraries
- blisterpeanuts 6y agoNice feature. Is there a way to globally enable/disable it? E.g., python3 myscript.py --enableFstringDebugging=true So that I could get rid of lots of conditional statements, e.g.: if debug: print (f"some useful debugging info")
- OJFord 6y agoUse a logger, not print, and set log level?
- the_jeremy 6y agoNo. You should use logging syntax[0] instead. [0]: https://docs.python.org/3/library/logging.html https://docs.python.org/3/library/logging.html
- grun 6y agoHey! I'm Ansgar. I wrote Icecream. f-strings's `=` is awesome. I'm overjoyed it was added to Python. I use it all the time. That said, IceCream does bring more to the table, like: - Stynax highlighting. - Pretty prints data structures. - Returns its value for nesting. - Integrates with logging via `ic.configureOutput()`. etc I hope that helps!
- polm23 6y agoThank you for writing IceCream! I've used it off and on over the years and it's been a great help.
- mcswell 6y agoI just got my first serving of icecream, thanks to the post here. And I'm a convert. One question: in most cases, I get an output like `ic| tmp.py:9 in nonesuch() at 03:23:52.479` What is the "at 03:23:52.479", and how do I turn it off? Looks like it's some sort of timer function, but I don't see it in the readme. Python3.7 on Linux.
- frays 6y agoThank you for sharing this.