1

I would like to debug my Python code by inspecting multiple variables, dumping out their names and contents, equivalent to Raku's dd (Raku was formerly known as "Perl 6"):

The closest I've found has been mentioned in another post, which compares Python's pprint to Perl 5's Data::Dumper. However, unlike dd, neither of those outputs the name of the variable. dd in Raku is closest to the show function from the Perl 5 module Data::Show, except show additionally outputs the filename and line number.

Here is a demo of Raku's dd in action:

#!/bin/env perl6

my %a = ( :A(1), :B(2) );
my %c = ( :C(3), :D(4) );

dd %a;
dd %c;

Which, when run, results in the following :

Hash %a = {:A(1), :B(2)}
Hash %c = {:C(3), :D(4)}

(By the way, a Hash in Perl or Raku is equivalent to a dictionary in Python)

And here is the closest I have yet been able to get in Python, but it redundantly requires both the name of the variable and the variable itself:

#!/usr/bin/env python

def tiny_dd(name,x):
    print(name + ' is "' + str(x) + '"')

a = { 'A':1, 'B':2}
c = { 'C':3, 'D':4}

tiny_dd('a',a)
tiny_dd('c',c)

Which, when run, results in the following:

a is "{'A': 1, 'B': 2}"
c is "{'C': 3, 'D': 4}"
Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
  • If you really want to use the variable name, and only state it once, then you could take advantages of the `locals` method. `def print_var(name): print(f"{name} is {json.dumps(locals()['name'])}")` – flakes Oct 06 '19 at 06:52

2 Answers2

3

Repeating a name twice when printing a value often grates people who think of variables as having unique values. In Python, however, it is often very hard to find names which reference a particular value, which makes writing a printer like the one you're looking for pretty hard, since you'd need do some very expensive looking around the name space.

That said, PySnooper has done all this heavy lifting for you and can print out a great deal of information on how a program is running, which can be very useful for debugging.

Note that in Python 3.8 you get pretty much what you're looking for with the new = syntax for f strings, which works like this (copied from the release notes):

>>> user = 'eric_idle'
>>> member_since = date(1975, 7, 31)
>>> f'{user=} {member_since=}'
"user='eric_idle' member_since=datetime.date(1975, 7, 31)"
chthonicdaemon
  • 19,180
  • 2
  • 52
  • 66
1

I would like to debug my Python code by inspecting multiple variables

Then you are probably better off using the built-in debugger, pdb.

If you must fall back on debug traces (my not-so-secret vice - just please make sure you don't check them in to version control) then hard-coding the variable name in a print call is not so bad - after all, maybe you can write something more descriptive than the variable name, anyway. There is also the pprint(prettyprint) module to get nicer formatting of complex nested data structures.

But if you really want to be able to find a variable given a string with its name, the built-in locals() and globals() functions provide dicts in which you can look up local and global variables respectively. You can also find global variables (attributes) of other modules (as well as attributes of anything else that has attributes) by name using the getattr() builtin function.

Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153