Preferred way of determining type

Bruce Sass bsass at freenet.edmonton.ab.ca
Wed May 16 16:38:11 EDT 2001


On Wed, 16 May 2001, Emile van Sebille wrote:

> More frequently lately it seems that isinstance(obj, types.Type) or simply
> if type(obj) == types.Type is recommended over what I regularly use, eg:  if
> type(obj) == type([]).
>
> Anyone care to explain why or in what circumstances there's a difference?

Have a look in /usr/lib/python*/types.py (or wherever Python is
installed), that is what happens when you do "import types".

I do what types.py does, and if it is easier to import all the types
than do it myself... I should probably re-think what I'm doing 'cause
I'm doing way too many type checks.

There are issues associated with doing (e.g.) "TypeDict = type({})" in
every module of your package (maybe someone who has actually played
with that aspect of it will get into details), which using "import
types" avoids.  I guess if you were really worried about efficiency
you could "import mytypes", where your mytypes.py is the subset of
types.py you are interested in.


- Bruce





More information about the Python-list mailing list