How to detect what type a variable is?

eduardo.padoan at gmail.com eduardo.padoan at gmail.com
Wed Nov 29 11:45:38 EST 2006


Leandro Ardissone wrote:
> Hi,
>
> I want to know what type is a variable.
> For example, I get the contents of an xml but some content is a list or
> a string, and I need to know what type it is.

You should try to treat it as a list, catch the exceptions raise when
it is a string (problably ValueError, TypeError ou Attribute error,
depends on what are you doing), and then treat it as a string. This is
the BAFP (better ask for forgiveness than permission) style, and is
more accepted in duck-typing languages like Python then LBYL (look
before you leep) style.




More information about the Python-list mailing list