where are isinstance types documented?

Simon Brunning simon at brunningonline.net
Tue Sep 26 06:37:37 EDT 2006


On 9/26/06, Simon Brunning <simon at brunningonline.net> wrote:
> On 26 Sep 2006 02:59:07 -0700, codefire <tony.bedford at gmail.com> wrote:
> > For example isinstance(a, int) works fine but isinstance(s, string)
> > doesn't - because 'string is not known'.
>
> In this case, you want "str" rather than "string".

A couple of points to consider:

Firstly, do you really need to use isinstance() at all? It's often not
necessary. Consider a duck-typing approach instead - i.e. to just try
using the object as a string, and to deal with exceptions arising from
the possibility that it isn't. This approach would allow string-like
objects as well as "real" strings.

Secondly, if you *really* want to use isinstance(), "basestring" is
the super-type of both "str" and "unicode", so it's more likely to be
what you want.

-- 
Cheers,
Simon B,
simon at brunningonline.net



More information about the Python-list mailing list