Var_dump python

When debugging in PHP, I commonly find it beneficial to simply stick a var_dump() in my code to present me what a variable is, what its worth is, and also the very same for anything that it contains.

You watching: Var_dump python

What is a great Python indistinguishable for this?

To screen a worth nicely, you can usage the pprint module. The simplest way to dump all variables via it is to do

from pprint import pprintpprint(globals())pprint(locals())If you are running in CGI, a advantageous debugging function is the cgitb module, which screens the value of local variables as component of the traceearlier.

I think the best tantamount to PHP"s var_dump($foo, $bar) is integrate print via vars:

print vars(foo),vars(bar)

The closest thing to PHP"s var_dump() is pprint() with the getmembers() feature in the built-in check module:

from check import getmembersfrom pprint import pprintpprint(getmembers(yourObj))

PHP"s var_export() typically reflects a serialized version of the object that deserve to be exec()"d to re-develop the object. The closest point to that in Python is repr()

"For many type of forms, this function makes an effort to rerevolve a string that would yield an object through the exact same value as soon as passed to eval() <...>"

I wrote a really light-weight different to PHP"s var_dump for using in Python and made it open resource later.


You have the right to sindicate install it using pip:

pip install var_dump

So I have taken the answers from this question and another question and also came up below. I suspect this is not pythonic sufficient for the majority of world, yet I really wanted somepoint that let me obtain a deep representation of the worths some unknown variable has. I would certainly appreciate any type of suggestions about how I have the right to improve this or attain the same habits less complicated.

def dump(obj): """return a printable depiction of an item for debugging""" newobj=obj if "__dict__" in dir(obj): newobj=obj.__dict__ if " object at " in str(obj) and also not newobj.has_key("__type__"): newobj<"__type__">=str(obj) for attr in newobj: newobj=dump(newobj) rerotate newobjHere is the usage

course stdClass(object): passobj=stdClass(),2,3,4)obj.dict="a":1,"b":2, "c":3, "more":"z":26,"y":25obj.list=<1,2,3,"a","b","c",<1,2,3,4>>obj.subObj=stdClass()obj.subObj.value="foobar"from pprint import pprintpprint(dump(obj))and also the outcomes.

"__type__": "", "dict": "a": 1, "c": 3, "b": 2, "more": "y": 25, "z": 26, "int": 1, "list": <1, 2, 3, "a", "b", "c", <1, 2, 3, 4>>, "subObj": "__type__": "", "value": "foobar", "tup": (1, 2, 3, 4)

Old topic, yet worth a try.

See more: Can You Apply Polyurethane Over Latex Paint ? Top 5 Best Clear Coat Over Latex Paint

Here is an easy and efficient var_dump function:

def var_dump(var, prefix=""): """ You recognize you"re a php developer when the first point you ask for as soon as discovering a new language is "Where"s var_dump?????" """ my_type = "<" + var.__class__.__name__ + "(" + str(len(var)) + ")>:" print(presettle, my_type, sep="") presettle += " " for i in var: if type(i) in (list, tuple, dict, set): var_dump(i, prefix) else: if isinstance(var, dict): print(presolve, i, ": (", var.__class__.__name__, ") ", var, sep="") else: print(preresolve, "(", i.__class__.__name__, ") ", i, sep="")Sample output:

I do not have actually PHP experience, yet I have actually an understanding of the Python traditional library.

For your functions, Python has actually a number of methods:

logging module;

Object serialization module which is called pickle. You might compose your very own wrapper of the pickle module.

If your using var_dump for testing, Python has its own doctest and unittest modules. It"s very basic and quick for architecture.

I usage self-created Printer course, but dir() is additionally good for outplacing the instance fields/worths.

course Printer: def __init__ (self, PrintableClass): for name in dir(PrintableClass): value = getattr(PrintableClass,name) if "_" not in str(name).join(str(value)): print " .%s: %r" % (name, value)The sample of usage:


Licensed under: CC-BY-SA through attribution
Not affiliated with StackOverflow
Sign up ×

Get answers to numerous questions and give ago by sharing your understanding through others.