Re: How to tell the difference between string and list

From: John J. Lee (jjl_at_pobox.com)
Date: 12/06/03


Date: 06 Dec 2003 15:48:27 +0000

bokr@oz.net (Bengt Richter) writes:

> On 05 Dec 2003 20:24:02 +0000, jjl@pobox.com (John J. Lee) wrote:
>
> >bokr@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



Relevant Pages

  • Virgin USB modem for Mobile Broadband in Australia - F10 Network Manager crash
    ... TypeError: exceptions must be classes, instances, or strings, not NotImplementedType ... Traceback: ...
    (Fedora)
  • F10 Network Manager crashes using "Edit" on ttyUSB0
    ... Traceback: ... TypeError: exceptions must be classes, instances, or strings, not NotImplementedType ...
    (Fedora)
  • Re: GPRS Connection
    ... It's the file with the strings that describe the various exceptions that a ... if you want a message that interprets ... the exception into a human-readable format, you need the localized strings ... You can get more detailed error information by installing this CAB. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Why Ruby?
    ... can raise exceptions. ... It seems to me encodings are less artifacts of *the* language and more ... reading a file opened in "rb" mode returns strings with encoding ... And the places I have encountered exceptions, ...
    (comp.lang.ruby)
  • Re: Structured exception information
    ... Or are you saying there's something specific about ... the "exceptions as objects" feature we're discussing that makes it hard ... But I fail to see how exceptions with Strings attached ...
    (comp.lang.ada)