Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Easy Q

0 views
Skip to first unread message

MRAB

unread,
Jan 9, 2010, 11:48:45 AM1/9/10
to python-list
Victor Subervi wrote:
> Hi;
> I have a string.join statement on a variable that comes from a
> cgi.FieldStorage().getlist. The variable may be a list or a single
> value. I need to treat it differently depending on which it is. How can
> I distinguish it? len(var) will obviously give me the length of the
> string if it's a string and the length of the list if it's a list.
>
1. string.join isn't a statement, it's a function, and why are you using
it anyway? Strings have a .join method:

>>> ", ".join(["first", "second", "third"])
'first, second, third'

2. Are you sure cgi.FieldStorage().getlist sometimes returns a single
value instead of a list? If that's the case then it's a very odd name
for the method!

Gary Herron

unread,
Jan 9, 2010, 11:56:12 AM1/9/10
to python-list
Victor Subervi wrote:
> Hi;
> I have a string.join statement on a variable that comes from a
> cgi.FieldStorage().getlist. The variable may be a list or a single
> value. I need to treat it differently depending on which it is. How
> can I distinguish it? len(var) will obviously give me the length of
> the string if it's a string and the length of the list if it's a list.
> TIA,
> beno

Like this:

if isinstance(var, list):
... join ...
else:
... ??? ...

Gary Herron

Dave Angel

unread,
Jan 9, 2010, 1:02:22 PM1/9/10
to Victor Subervi, python-list
Victor Subervi wrote:
> Hi;
> I have a string.join statement on a variable that comes from a
> cgi.FieldStorage().getlist. The variable may be a list or a single value. I
> need to treat it differently depending on which it is. How can I distinguish
> it? len(var) will obviously give me the length of the string if it's a
> string and the length of the list if it's a list.
> TIA,
> beno
>
>
If you have to do an explicit type check of your variable, use an if
statement, and the isinstance() built-in function. Note that you can
check for multiple types, like list and tuple, in the same function call.


DaveA

Jean-Michel Pichavant

unread,
Jan 11, 2010, 9:00:40 AM1/11/10
to Victor Subervi, python-list
Victor Subervi wrote:

> On Sat, Jan 9, 2010 at 11:56 AM, Gary Herron
> <ghe...@islandtraining.com <mailto:ghe...@islandtraining.com>> wrote:
>
> Victor Subervi wrote:
>
> Hi;
> I have a string.join statement on a variable that comes from a
> cgi.FieldStorage().getlist. The variable may be a list or a
> single value. I need to treat it differently depending on
> which it is. How can I distinguish it? len(var) will obviously
> give me the length of the string if it's a string and the
> length of the list if it's a list.
> TIA,
> beno
>
>
> Like this:
>
> if isinstance(var, list):
> ... join ...
> else:
> ... ??? ...
>
>
> Right.. Thanks!
> beno
You should definitely check again MRAB's answer.
Having getlist returning something else than a list, especially a non
iterable single item *is* suspicious.
Usually, properly designed functions return consistent types over calls.

JM

Gabriel Genellina

unread,
Jan 13, 2010, 12:58:00 AM1/13/10
to pytho...@python.org
En Sat, 09 Jan 2010 13:11:28 -0300, Victor Subervi
<victor...@gmail.com> escribiᅵ:

> Hi;
> I have a string.join statement on a variable that comes from a
> cgi.FieldStorage().getlist. The variable may be a list or a single
> value. I
> need to treat it differently depending on which it is. How can I
> distinguish
> it? len(var) will obviously give me the length of the string if it's a
> string and the length of the list if it's a list.

Are you sure you have tested with getlist()?
getlist() *always* returns a list: an empty one, a list containing a
single item, or many items. getvalue() on the other hand may return None,
a single item, or a list.

--
Gabriel Genellina

0 new messages