[Tutor] Type Checking:/High-Jacking Reserved Words
D-Man
dsh8290@rit.edu
Mon, 5 Mar 2001 16:53:56 -0500
On Sun, Mar 04, 2001 at 08:16:06AM -0900, Tim Johnson wrote:
| I would welcome some comments from anyone as to the
| comparative advantages of the following
| > type(mystr) == types.StringType
| as opposed to
| > type(mystr) == type('string'):
| because the first method necessitates importing another
| module: re: more "overhead".
I also prefer the first form, even though I didn't know about the
types module until now (I haven't needed it yet). I was idly
wondering about checking the type of instance objects and the overhead
of creating a new instance. Take for example,
class Foo :
def __init__( self ) :
sleep( 50000 ) # take some time initing the object ;-)
obj = Foo()
if type( obj ) is type( Foo() ) :
print "matches"
This would take execessive time (and maybe resources) to create an
instance of Foo solely to check its type. I like the
isinstance( obj , Foo ) method since it avoids this unnecessary
overhead. I suppose one could also do
if obj.__class__ is Foo :
print "matches"
but I don't think access __class__ is very clean.
-D