[Tutor] How do I do this in python?

Robert Lummis robert.lummis at gmail.com
Thu Jun 11 23:08:57 CEST 2009


On Thu, Jun 11, 2009 at 11:58 AM, spir<denis.spir at free.fr> wrote:
> Le Thu, 11 Jun 2009 10:46:26 -0400,
> Kent Johnson <kent37 at tds.net> s'exprima ainsi:
>
>> On Wed, Jun 10, 2009 at 9:43 PM, Robert Lummis<robert.lummis at gmail.com>
>> wrote:
>> > I want to write a function that I can use for debugging purposes that
>> > prints the values of whatever list of object references it is given as
>> > arguments, without knowing in advance how many object references it
>> > will be called with or what they might be. For example, the call:
>> > show(a,b,c) would output the values of the arguments like this:
>> >
>> >    a = 3
>> >    b = 'john'
>> >    c = 'Monday'
>> >
>> > while show (x) would output (for example):
>> >
>> >    x = 3.14
>>
>> Here is a pretty clean solution. It passes names rather than values,
>> then looks the values up in the caller's stack frame. Written for
>> Python 2.x but should work for 3.x if you change the prints.
>>
>> In [1]: import sys
>> In [11]: def show(names):
>>    ....:     frame = sys._getframe(1)
>>    ....:     for name in names.split():
>>    ....:         if name in frame.f_locals:
>>    ....:             print name, '=', frame.f_locals[name]
>>    ....:         else:
>>    ....:             print name, 'not found'
>>
> [...]
>>
>> Kent
>
> Waow, great!
>
> Denis
> ------
> la vita e estrany
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>

Thanks for all the intelligent and thoughtful replies to my newB
question. It looks like this tutor mailing list is going to be a big
help!  It's going to take me some time and trial and error to digest
all the replies so I understand them fully but it will be instructive.

I guess one thing I learned already, even though I didn't ask about
it, is that using python3 isn't the best way to start out.

Again, thank you all for taking the time to reply.
-- 
Robert Lummis


More information about the Tutor mailing list