Newbie help - test for data type
Jonathan Bowlas
me at jonbowlas.com
Mon Jul 31 05:32:48 EDT 2006
Thanks for everyone's help, much appreciated, I'll check out the isinstance
function.
Jon
-----Original Message-----
From: python-list-bounces+info=jonbowlas.com at python.org
[mailto:python-list-bounces+info=jonbowlas.com at python.org] On Behalf Of
Dennis Benzinger
Sent: 31 July 2006 10:20
To: python-list at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060731/574f02b0/attachment.html>
More information about the Python-list
mailing list