[silva-dev] Find out data type

Andy Altepeter aaltepet at bethel.edu
Thu May 13 18:30:28 CEST 2004


Hi Sam,

I've struggled with this in my rectricted python code.  Normally you can
use isinstance or type(object).  The standard types module (see
python.org/docs) contains the stanard types like list,string, tuple,
etc.

Restricted code doesn't have access to the types module.  One can do so,
but I think it's considered a hack?

Anyways, the way I determine whether a var is an object or a list of
objects is like this:

if hasattr(var,'sort'): #assume var is a list

When you know var is either a list or a string, then there isn't a
problem with the above, as strings don't have a 'sort' method. You could
also test for any other method lists have but strings don't.

Everyone else can feel free to correct me on this programming practice
;-)

HTH,
Andy

On Thu, 2004-05-13 at 11:26, Samuel Schluep wrote:
> Dear developers
> 
> How can I find out if a Python variable contains a string or a list of 
> strings. Both is possible, but needs to be handled differently. Any 
> suggestions.
> 
> Thanks and best regards
> Sam




More information about the silva-dev mailing list