473,399 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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 1626
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
10
by: jeniffer | last post by:
char *a="this is a string"; char a="this is a string"; first is a pointer while second is an array.Tell me more differences
13
by: Jiho Han | last post by:
Here's the issue. You have a class, Class Person { public int id; public string firstname; public string lastname; }
2
by: lincoln rutledge | last post by:
I'm having trouble figuring out the difference between a string and a list. I know that: string = "foo bar" is a list of characters, "foo bar", and string is "f". while:
9
by: Stephan Steiner | last post by:
Hi I seem to have a bit of trouble understanding one bit of how generics work: In C#, every class automatically derives from object, and inherits a bunch of properties (i.e. ToString()). Thus,...
0
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi just wondering if anyone knows if there is a way to tell if a child window is still open in the code behind in the parent window (web application vs.net 2005)? I have a web app and am using the...
4
by: Tom P. | last post by:
What would the difference be between the following two: public class Foo : IList<string> { .... } public class Foo : List<string>
11
by: cmb3587 | last post by:
I have two arrays and I'm trying to create a 3rd array that is the difference between the two arrays Ex: arrayA: 3 5 8 9 arrayB: 3 4 6 9 difference of A-B: 5 8 however, my...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.