Determining Types
Ignacio Vazquez-Abrams
ignacio at openservices.net
Sat Sep 1 14:12:17 EDT 2001
On Sat, 1 Sep 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
The problem you're having is that type() doesn't return a string, it returns a
type object, which you then have to compare to the objects in the types
module.
---
import types
...
if type(a)==types.IntType:
print 'a is an integer'
elif type(a)==types.StringType:
print 'a is a string'
---
A shortcut that I've seen is to just compare it to another type() call:
---
if type(a)==type(0):
print 'a is an integer'
elif type(a)==type(''):
print 'a is a string'
---
--
Ignacio Vazquez-Abrams <ignacio at openservices.net>
More information about the Python-list
mailing list