Michael Langford wrote: >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))
I was thinking more or less along the same lines, but - you don't need the copy() - locals is a dict mapping names to values, so something like for name, value in locals().iteritems(): print "varname: %s type: %s" (name,type(value)) would work better - Since the OP is trying to find cases where the same variable is assigned different types, presumably in a single scope, checking just at the end of a function won't help. > 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. Not sure how you would do that with unit tests? > Or you can absolutely litter your code with this loop and > go through the output for lines that are wrong. That is basically what you could do with settrace(). Rather than printing a bunch of output, perhaps maintaining a dict that maps name to a set of types would work. settrace() notifies it's target when a scope is entered or exited so you could monitor values in each scope separately. It gets tricky though, with things like this: def square(x): return x*x square(1) # x is an integer square(1+1j) # x is a complex number or def foo(): for i in range(3): print i # integer for j in range 3: i = j/2.0 # different type in a different block but same scope print i # float, is this an error? Bottom line: if you can define clearly what you want, there is probably a way to get it with settrace() and locals() but it is not a simple problem. Kent _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor