469,282 Members | 1,732 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

How to tell the difference between string and list

Hello,

I need to recognize 'var' and ['var'], usually I would use:

if a.__class__() == '':
#string
elif a.__class__() == []:
#list

But unfortunately in Zope PythonScripts, where I need to use this, one
is not supposed to use underscore-prefixed methods as I just found out.

I figure this is a question general enough to post here (and not on the
Zope list), any clues?

TIA

Jan Kokoska


Jul 18 '05 #1
4 1485
Heyho!

Jan Kokoska wrote:
[...]

But unfortunately in Zope PythonScripts, where I need to use this, one
is not supposed to use underscore-prefixed methods as I just found out. In Zope you can use same_type():
same_type(a, '')
same_type(a, [])
(See Zope Book -> Advanced Zope Scripting -> Built-in Functions)
I figure this is a question general enough to post here (and not on the
Zope list), any clues?

TIA

Jan Kokoska

HTH,
Wolfram

Jul 18 '05 #2
Jan Kokoska <ko*********@globe.cz> writes:
Hello,

I need to recognize 'var' and ['var'], usually I would use:

if a.__class__() == '':
#string
elif a.__class__() == []:
#list


That's a very strange way of doing it in any situation!

Cheers,
mwh

--
This is an off-the-top-of-the-head-and-not-quite-sober suggestion,
so is probably technically laughable. I'll see how embarassed I
feel tomorrow morning. -- Patrick Gosling, ucam.comp.misc
Jul 18 '05 #3
Jan Kokoska <ko*********@globe.cz> writes:
I need to recognize 'var' and ['var'], usually I would use:

[...]

All the other solutions posted are bad because they fail for
user-defined string-like classes (unless those classes use the new 2.2
features by deriving from str or unicode). As long as your strings
aren't huge:

def isstringlike(x):
try: x+""
except TypeError: return False
else: return True
I think I stole this off Alex Martelli.
John
Jul 18 '05 #4
bo**@oz.net (Bengt Richter) writes:
On 05 Dec 2003 20:24:02 +0000, jj*@pobox.com (John J. Lee) wrote:
bo**@oz.net (Bengt Richter) writes:
[...]
>>> s2 = ['abc','def'][...]>
>>> s2 += 'ghi'
>>> s2
['abc', 'def', 'g', 'h', 'i']


That's not what the code I posted tests for.


You mean __iadd__? I know, I was just showing ordinary list
behavior, and showed that as well to imply that that's also
something you might want to override in a consistent way if you were
overriding __add__ as in my example.


What? list.__add__(self, "") raises TypeError, which means lists do
work OK with isstringlike().

[...]
Don't forget, an object's __add__ could be defined to accept
_anything_ for addition, and a string would just be a generic thing,
whose stringness was really irrelevant to the adding object. Are you
making other assumptions based on isstringlike?
That's true: especially given the sequence-ness of strings, which was
the whole motivation for this function in the first place! In fact,
isstringlike(UserList()) is true. Bah.

If so, I think those assumptions should be an explicit part of the
test, or you are setting yourself up for using the test and assuming
the wrong things about some object. You can say you'll use
exceptions will sort those things out,

[...]

Well, sometimes that's just the problem: exceptions *won't* get
raised, because strings are sequences.

I guess it's all depends on the particular case. :-(
John
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by John M. Gabriele | last post: by
10 posts views Thread by jeniffer | last post: by
2 posts views Thread by lincoln rutledge | last post: by
9 posts views Thread by Stephan Steiner | last post: by
reply views Thread by =?Utf-8?B?UGF1bA==?= | last post: by
11 posts views Thread by cmb3587 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.