Newbie help - test for data type
Dennis Benzinger
Dennis.Benzinger at gmx.net
Mon Jul 31 05:20:23 EDT 2006
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
More information about the Python-list
mailing list