Determining Types

Emile van Sebille emile at fenx.com
Sat Sep 1 13:55:19 EDT 2001


>>> import types
>>> dir (types)
['BufferType', 'BuiltinFunctionType', 'BuiltinMethodType', 'ClassType',
'CodeType', 'ComplexType', 'DictType', 'DictionaryType', 'EllipsisType',
'FileType', 'FloatType', 'FrameType', 'FunctionType', 'InstanceType',
'IntType', 'LambdaType', 'ListType', 'LongType', 'MethodType', 'ModuleType',
'NoneType', 'SliceType', 'StringType', 'TracebackType', 'TupleType',
'TypeType', 'UnboundMethodType', 'UnicodeType', 'XRangeType',
'__builtins__', '__doc__', '__file__', '__name__']
>>> isinstance('a', types.StringType)
1
>>> isinstance('a', types.IntType)
0
>>> isinstance(123, types.IntType)
1
>>>

IIRC, this is the preferred method.  However, the following is still in use:

>>> var = 'hello'
>>> type(var) == type('a')
1
>>>

HTH,

--

Emile van Sebille
emile at fenx.com

---------
"Adonis Vargas" <deltapigz at telocity.com> wrote in message
news:fe9k7.1835$ix.446807 at newsrump.sjc.telocity.net...
> 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
>
>




More information about the Python-list mailing list