473,381 Members | 1,573 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,381 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 1625
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...

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.