472,374 Members | 1,319 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 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 1576
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.