From: Helge Stenström on
I want to write function that prints a value of a variable, for
debugging. Like:

with

myVariable = "parrot"
otherVariable = "dead"

probe(myVariable)
probe(otherVariable)

instead of the longer

print "myVariable = ", myVariable
print "otherVariable = ", otherVariable

Is that even possible?

The function would look like

def probe(var):
nameOfVar = someMagic(var)
print nameOfVar, " = ", var

but can someMagic(var) find the variable name? Is that possible?
From: Gary Herron on
Helge Stenström wrote:
> I want to write function that prints a value of a variable, for
> debugging. Like:
>
> with
>
> myVariable = "parrot"
> otherVariable = "dead"
>
> probe(myVariable)
> probe(otherVariable)
>
> instead of the longer
>
> print "myVariable = ", myVariable
> print "otherVariable = ", otherVariable
>
> Is that even possible?
>
> The function would look like
>
> def probe(var):
> nameOfVar = someMagic(var)
> print nameOfVar, " = ", var
>
> but can someMagic(var) find the variable name? Is that possible?
>

That question really doesn't quite make sense. Consider all the
ways such a function can be called:
someMagic(42)
someMagic(40+2)
someMagic(f(123))
someMagic(*argTuple)
someMagic(**kwDict)
someMagic(array[42])
None of which have a variable name associated with the argument.

Yet, the answer to your question is not quite absolutely "no". Python
has lots of introspection capabilities, including, perhaps, getting at
and parsing the original code to find the call. But there's nothing
direct for what you want.

Gary Herron





From: Gregory Ewing on
Helge Stenström wrote:

> I want to write function that prints a value of a variable, for
> debugging. Like:
>
> myVariable = "parrot"
> otherVariable = "dead"
>
> probe(myVariable)
> probe(otherVariable)

Not exactly, but you can come close with a little hackery.

import sys

def print_var(name):
print name, "=", sys._getframe(1).f_locals[name]

def f():
fred = 42
mary = "christmas"
print_var("fred")
print_var("mary")

f()

--
Greg
From: Phlip on
> Yet, the answer to your question is not quite absolutely "no".  Python
> has lots of introspection capabilities, including, perhaps, getting at
> and parsing the original code to find the call.    But there's nothing
> direct for what you want.
>
> Gary Herron

Below my sig is one shot at it; which requires a lambda: for other
reasons. It's an assertion used with Django models.

You can search for the name of your trace method, instead of lambda,
using this brute-force technique.

And here's a nice thread on Greg's technique, from a very short time
ago:

http://groups.google.com/group/comp.lang.python/msg/03dd85ce009044e9

--
Phlip
http://penbird.tumblr.com/

def assert_model_changes(self, mod, item, frum, too, lamb):
source = open(lamb.func_code.co_filename, 'r').readlines()
[lamb.func_code.co_firstlineno - 1]
source = source.replace('lambda:', '').strip()
model = str(mod.__class__).replace("'>", '').split('.')[-1]

should = '%s.%s should equal `%s` before your activation line,
`%s`' % \
(model, item, frum, source)

self.assertEqual(frum, mod.__dict__[item], should)
lamb()
mod = mod.__class__.objects.get(pk=mod.pk)

should = '%s.%s should equal `%s` after your activation line, `
%s`' % \
(model, item, too, source)

self.assertEqual(too, mod.__dict__[item], should)
return mod
From: Tino Wildenhain on
Hi,

Am 14.03.2010 12:58, schrieb Helge Stenström:
> I want to write function that prints a value of a variable, for
> debugging. Like:
>
> with
>
> myVariable = "parrot"
> otherVariable = "dead"
>
> probe(myVariable)
> probe(otherVariable)
>
> instead of the longer
>
> print "myVariable = ", myVariable
> print "otherVariable = ", otherVariable
>
> Is that even possible?
>
> The function would look like
>
> def probe(var):
> nameOfVar = someMagic(var)
> print nameOfVar, " = ", var
>
> but can someMagic(var) find the variable name? Is that possible?

apart from very hackery, why don't you just use:

def probe(**vars):
for varname,value in vars.items():
print "%s = %r" % (varname,value)

and call it like that:

probe(myvar="foo")
probe(othervar="bar")

or even

probe(myvar=myvar) (if myvar was assigned before)

please note introspective approaches have a very narrow field
where they can work - as already shown in other posts: you can have
values w/o names and also more then one name to a value (or object).

Regards
Tino Wildenhain