Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #19669 > unrolled thread
| Started by | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| First post | 2012-01-31 19:56 -0500 |
| Last post | 2012-02-02 07:31 +0000 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
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
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-01-31 19:56 -0500 |
| Subject | Re: How can I verify if the content of a variable is a list or a string? |
| Message-ID | <mailman.5271.1328057831.27778.python-list@python.org> |
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
[toc] | [next] | [standalone]
| From | Rainer Grimm <r.grimm@science-computing.de> |
|---|---|
| Date | 2012-02-01 23:19 -0800 |
| Message-ID | <mailman.5346.1328167206.27778.python-list@python.org> |
| In reply to | #19669 |
You can do it more concise.
>>> def isListOrString(p):
... return any((isinstance(p,list),isinstance(p,str)))
...
>>> listOrString("string")
True
>>> listOrString([1,2,3])
True
>>> listOrString(2)
False
>>> listOrString(False)
False
Rainer
[toc] | [prev] | [next] | [standalone]
| From | Rainer Grimm <r.grimm@science-computing.de> |
|---|---|
| Date | 2012-02-01 23:19 -0800 |
| Message-ID | <8416844.281.1328167197216.JavaMail.geo-discussion-forums@vby1> |
| In reply to | #19669 |
You can do it more concise.
>>> def isListOrString(p):
... return any((isinstance(p,list),isinstance(p,str)))
...
>>> listOrString("string")
True
>>> listOrString([1,2,3])
True
>>> listOrString(2)
False
>>> listOrString(False)
False
Rainer
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-02-02 07:31 +0000 |
| Message-ID | <4f2a3bc4$0$29895$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #19773 |
On Wed, 01 Feb 2012 23:19:57 -0800, Rainer Grimm wrote: > You can do it more concise. > >>>> def isListOrString(p): > ... return any((isinstance(p,list),isinstance(p,str))) Or even more concisely still: isinstance(p, (list, str)) -- Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web