Typing question

Bob van der Poel bvdpoel at uniserve.com
Fri Jun 16 23:17:53 EDT 2000


I'm a bit confused about how python decides on the type for a variable.
I was writing a simple program to parse a data file and print some
results. What I found was that if I did something like:

	a = 123
	print " %.2f " % a

All worked just fine.

However, when I read in a line of data from a file, and then converted
the data into a list using split() the types of the data (which are
numeric) stayed as 'string'. So, the resulting print() failed (actually
it was a bit more complicated since some arithmetic was involved as
well, but that obscures the point).

I see that what is happening: the data is read as a string, split()
converts a string into STRING list elements, and when I do something
like a=list[1] 'a' is just a pointer to the list element (I'm a bit
fuzzy on this last item).

So, I end up forcing the conversion using float() when assigning the
results of split() to the storage list. Is this the correct way to do
things in python?


-- 
   __
  /  )      /         Bob van der Poel
 /--<  ____/__        bvdpoel at uniserve.com
/___/_(_) /_)         http://users.uniserve.com/~bvdpoel




More information about the Python-list mailing list