Determining Types

Joal Heagney s713221 at student.gu.edu.au
Sat Sep 1 22:13:55 EDT 2001


Adonis Vargas wrote:
> 
> how am i able to determine types of variables? i have come up with the
> type() function but am unsuccessful in using it. this is the code in which
> im using the provided function:
> 
> def convert():
>     if type(txth1.get()) == 'int':
>         print 'its an int'
>     elif type(txth1.get()) == 'str':
>         print 'its a string'
> 
> any help would be greatly appreciated.
> 
> Adonis

I think in 2.2 this works, but I think you have to compare to the actual
objects, not strings. I upgraded my machine a few weeks ago, and haven't
gotten around to rebuilding python2.2 so I can't be definite.

def convert():
	if type(txth1.get()) == int:
		print 'its an int'
	elif type(txth1.get()) == str:
		print 'its a string'

The clue that gave away that you were working on 2.2 was that you were
comparing to 'str' rather than 'string'. In pre-2.2 versions,
>>>import types
>>> types.StringType
<type 'string'>

But in 2.2, the returned types were set up so that if you called them,
they returned an instance of that type, so 'string' became 'str'
-- 
      Joal Heagney is: _____           _____
   /\ _     __   __ _    |     | _  ___  |
  /__\|\  ||   ||__ |\  || |___|/_\|___] |
 /    \ \_||__ ||___| \_|! |   |   \   \ !



More information about the Python-list mailing list