[Tutor] Type Checking:/High-Jacking Reserved Words

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 4 Mar 2001 19:08:58 -0800 (PST)


On Sun, 4 Mar 2001, Sheila King wrote:

> It is not necessary to import the types module.
> 
> Just try this:
> 
> teststr = ""
> 
> if type(mystr) == type(teststr):
>     pass
> 
> People pointed this out yesterday, but perhaps you overlooked it?
> I reckon, that this is what the types module does, anyhow.

Let's take a look:

[excerpt from types.py]
###
IntType = type(0)
LongType = type(0L)
FloatType = type(0.0)
try:
    ComplexType = type(complex(0,1))
except NameError:
    pass

StringType = type('')
###

Yes, that's how they're doing it.  Personally, I like using the StringType
approach, but both will work ok.