Newbie help - test for data type

Rob Wolfe blue99 at interia.pl
Mon Jul 31 05:33:50 EDT 2006


> This is my function:
> selecteddeptcodes = context.REQUEST.DEPTCODE
> currentstatus = context.REQUEST.STATUS
>
> if currentstatus == 'pending':
>  for dptcd in selecteddeptcodes:
>    context.changetolive(DEPTCODE=dptcd)
> if currentstatus == 'old':
>  for dptcd in selecteddeptcodes:
>    context.changetopending(DEPTCODE=dptcd)
> return context.pub_dept_form(context, context.REQUEST, message='Updated
> Status')
>
> The argument in question is selecteddeptcodes.

You can use isinstance or function like that:

>>> def list_from_string(s):
...     try:
...             s + ''
...             return [s]
...     except:
...             return s

and then:

>>> def f(selecteddeptcodes):
...     if selecteddeptcodes is None: return
...     selecteddeptcodes = list_from_string(selecteddeptcodes)
...     for dptcd in selecteddeptcodes: print dptcd
...
>>> f(['aaa', 'bbb'])
aaa
bbb
>>> f(['aaa'])
aaa
>>> f('aaa')
aaa
>>> f(None)
>>> 


Regards,
Rob




More information about the Python-list mailing list