Re: how do I know if it is a sequence or mapping type?

Guido.van.Rossum@cwi.nl
Fri, 08 Jul 1994 09:31:26 +0200

> I'm a Python newbie working on a browser that lets you examine
> variables and what they point to, etc. I'm working with NEXTSTEP so I
> doubt it's been done before on this platform. Anyway I'm trying to figure
> out whether a given object is a container object and if so whether it is a
> sequence type or mapping type. For instance if someone handed me an array
> how could I know that it is a sequence type if I don't have my own table
> that tells me that an array is a sequence.

Use the (built-in) type() function -- it returns a type object. The
current convention to test for a list, for instance, is to write

if type(mystery_object) == type([]):
print "It's a list!!!"

Have a look at the standard module repr for some hints -- it may do a
lot of what you're trying to write already...

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>
URL: <http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>