Frage

In ruby you can inspect any object with the inspect method:

For example:

print [1,"string",:symbol,[?l, ?i, ?s, ?t]].inspect

will print

[1, "string", :symbol, ["l", "i", "s", "t"]]

Is there any similar facility in python which allows me to print the content of some arbitrary variable?

War es hilfreich?

Lösung

Use repr. It returns a string containing a printable representation of an object. (similar to Object#inspect in Ruby)

>>> repr([1,"string", ':symbol', ['l', 'i', 's', 't']])
"[1, 'string', ':symbol', ['l', 'i', 's', 't']]"

BTW, there's no symbol literal (:symbol) or single character string literal (?x) in Python; replaced them with string literals in the above example.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top