[Tutor] DATA TYPES

Kent Johnson kent37 at tds.net
Wed Feb 20 21:26:04 CET 2008


Toby wrote:
> As I understand it python is not a strongly typed language so no declaration
> of variables is necessary.

Actually Python has strong, dynamic typing but I guess that is a 
discussion for another thread.

> If I use a variable in a program that stores certain numbers and I'm porting
> it to say ... java

Do you know about Jython? Maybe you don't have to port it.

> where I must first declare the variables before I use
> them how do I find out what type to give that variable, specifically is
> there a way to identify the type of a variable used in a python program for
> reference?

Not easily.

> What I'm saying is if 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?  I realize I could trudge through the
> entire 1000 lines and check all the variables and see what values I have
> assigned them one at a time but I'm wondering if there is an easier way to
> get that data?

I guess this is possible in principle at least using sys.settrace():
http://docs.python.org/lib/debugger-hooks.html#debugger-hooks

For example CodeInvestigator gathers the information you want, though it 
doesn't present it in the fashion you want. You could look at how they 
do it...the trace module is another example.
http://codeinvestigator.googlepages.com/main
http://docs.python.org/lib/trace-api.html

Kent


More information about the Tutor mailing list