Re: Newbie help - test for data type
- From: Dennis Benzinger <Dennis.Benzinger@xxxxxxx>
- Date: Mon, 31 Jul 2006 11:20:23 +0200
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
.
- Follow-Ups:
- Re: Newbie help - test for data type
- From: Bruno Desthuilliers
- Re: Newbie help - test for data type
- References:
- Newbie help - test for data type
- From: Jonathan Bowlas
- Newbie help - test for data type
- Prev by Date: Re: Newbie help - test for data type
- Next by Date: Re: Has anyone used davlib by Greg Stein?
- Previous by thread: Re: Newbie help - test for data type
- Next by thread: Re: Newbie help - test for data type
- Index(es):
Relevant Pages
|