Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19669

Re: How can I verify if the content of a variable is a list or a string?

From Terry Reedy <tjreedy@udel.edu>
Subject Re: How can I verify if the content of a variable is a list or a string?
Date 2012-01-31 19:56 -0500
References <1328057052.56088.YahooMailNeo@web30601.mail.mud.yahoo.com>
Newsgroups comp.lang.python
Message-ID <mailman.5271.1328057831.27778.python-list@python.org> (permalink)

Show all headers | View raw


On 1/31/2012 7:44 PM, Andres Soto wrote:
> Hi,
> I'm writing a function which receive a list which elements are strings
> or new lists (sublists) containing strings.
> How can I verify if sone element of the list (which is contained in a
> variable) is a list or a string?
> I found the method isinstance(object,class) but I don't know which class
> should I use for.

For 3.x, 'list' or 'str' (where 'str' means unicode). For 2.x, I believe 
'basestring' includes unicode strings as well as byte strings.

 >>> isinstance([], list)
True
 >>> isinstance([], str)
False

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: How can I verify if the content of a variable is a list or a string? Terry Reedy <tjreedy@udel.edu> - 2012-01-31 19:56 -0500
  Re: How can I verify if the content of a variable is a list or a string? Rainer Grimm <r.grimm@science-computing.de> - 2012-02-01 23:19 -0800
  Re: How can I verify if the content of a variable is a list or a string? Rainer Grimm <r.grimm@science-computing.de> - 2012-02-01 23:19 -0800
    Re: How can I verify if the content of a variable is a list or a string? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-02 07:31 +0000

csiph-web