[Tutor] DATA TYPES

Michael Langford mlangford.cs03 at gtalumni.org
Wed Feb 20 21:53:06 CET 2008


On Wed, Feb 20, 2008 at 12:41 PM, Toby <toby.pas at gmail.com> wrote:
> I have used a certain variable to hold literally
>  dozens of different values and I BELIEVE them all to be integers but I'm not
>  entirely sure that I have not stumbled into the float realm is there a way
>  to have python list all variables contained in a program and tell me what
>  type it has internally stored them as?

fyi, it sounds like you did something bad here. Reusing variables to
stand in for several different things, in general, a bad idea. It is
very confusing to humans (including you, later on). You should try to
keep names alive for a short a distance as possible (there is a metric
for this, called span, which you should try to minimize. There are
tools that help you practice this).

Now Kent can probably testify to as to if this is a good idea (he
knows a lot more about these python innards than I do),  but you can
print out the type of the local variables at the end of your
functions.

i.e.:

for var in locals().copy():
    print "varname: %s type: %s" (var,type(var))

After you've done that, you can see what type is referred to by each
name at the end of the function, then with some unit tests you should
be able to tell if you temporarily changed to a different data type in
the middle. If nothing else, you should be able to write the unit
tests in jython/cpython compatable code so you don't have to write
them twice. Or you can absolutely litter your code with this loop and
go through the output for lines that are wrong.

               --Michael

-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.RowdyLabs.com


More information about the Tutor mailing list