Thanks for everyone’s help, much appreciated, I’ll check out the isinstance function.

 

Jon

 

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Dennis Benzinger
Sent: 31 July 2006 10:20
To: python-list@python.org
Subject: Re: Newbie help - test for data type

 

Jonathan Bowlas wrote:

> Hi Listers,

>

> I have a requirement to test for a data type could someone tell me if this

> is possible in python?

>

> Basically I have a ZPT in Zope that users can select checkboxes in a form

> which pass arguments for a python function, however if there is only one

> checkbox selected it is passed as a string whereas more than one checkbox is

> passed as a list. Therefore if my function is required to perform an action

> based on each argument passed in the list the function works correctly but

> if it is passed as a string nothing happens.

 

You need the isinstance() function. For example you can use

isinstance(selecteddeptcodes, list) to test if your variable is a list.

If you want to test the other way round use

isinstance(selecteddeptcodes, str) if your variable is a string,

isinstance(selecteddeptcodes, unicode) if it's a unicode string or

isinstance(selecteddeptcodes, basestring) to test if it's a string or

unicode string. Read more about this function and the types to test for

in http://docs.python.org/lib/built-in-funcs.html.

 

If you already have the code for a list argument I'd check if it's not a

list and then turn it into a list:

 

 

if not isintance(selecteddeptcodes, list):

     selecteddeptcodes = [selecteddeptcodes]

 

 

 

Bye,

Dennis

--

http://mail.python.org/mailman/listinfo/python-list

 

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to