testing for class of instance

Martin von Loewis loewis at informatik.hu-berlin.de
Wed May 16 19:20:32 CEST 2001


"Stephen Hansen" <news at myNOSPAM.org> writes:

>     Your example doesn't exactly follow your question. To determine if a
> perticular object is the instance of a perticular class, you can use the
> isinstance() function... your example, though, wishes to check for the type
> of an object, which is unfortunately a different issue.
> 
>     from types import *
> 
>     if type(object) is DictType:
> 
> is the correct way to test if an object is a dictionary, which is what your
> example wants to do.

It is correct, but I question whether it is the best way.

    from types import DictionaryType
    if type(object) is DictionaryType

is better, IMO: it does not use 'import *', and it spells out Dictionary.

Even better is

    if isinstance(object, DictionaryType):

Regards,
Martin



More information about the Python-list mailing list