[Tutor] validation

Kent Johnson kent37 at tds.net
Mon Aug 27 13:31:01 CEST 2007


Alan Gauld wrote:
> "Michael" <python at mrfab.info> wrote
>> trying to use the Type() function but I cannot work out how to check 
>> the
>> return value? Caomparing it to 'int' or 'str' isn't working,
> 
> The easiest way is to compare to another type:
> 
> x = 42
> if type(x) == type(int()):
> 
> or even
> 
> if type(x) == type(2):

For the built-in types, since Python 2.2 the familiar name (int, str, 
float, list, dict, set) *is* the type and you can compare to that 
directly, e.g.:

In [13]: type(3)==int
Out[13]: True
In [14]: type([]) == list
Out[14]: True

Kent


More information about the Tutor mailing list