Re: How to tell the difference between string and list
From: John J. Lee (jjl_at_pobox.com)
Date: 12/06/03
- Next message: Jeroen van der Ham: "Reading pipes in Python"
- Previous message: Ron Adam: "Re: Lists and Tuples"
- In reply to: Bengt Richter: "Re: How to tell the difference between string and list"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Jeroen van der Ham: "Reading pipes in Python"
- Previous message: Ron Adam: "Re: Lists and Tuples"
- In reply to: Bengt Richter: "Re: How to tell the difference between string and list"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|