On Mon, Jul 21, 2008 at 1:06 PM, Eric Firing <[EMAIL PROTECTED]> wrote: > John Hunter wrote: >> >> On Mon, Jul 21, 2008 at 7:51 AM, David Kaplan <[EMAIL PROTECTED]> wrote: >> >>> 2) Can someone explain to me why is_string_like in the cbook doesn't >>> just do isinstance(obj,str)? Is there anything "string like" that won't >>> be caught be this isinstance call? >> >> In [65]: s = u'jdh' >> >> In [66]: isinstance(s, str) >> Out[66]: False >> >> In [67]: isinstance(s, unicode) >> Out[67]: True >> >> So we could check for str or unicode, but a user may be using a custom >> string like class from some c++ extension code that is part of a large >> in house API. The point is that we don't care if it *is* a string, we >> just want it to act like a string >> >> http://en.wikipedia.org/wiki/Duck_typing > > Sometimes we have > > if is_string_like(s) and s == 'some string': do_something() > > I think that as long as we know s is not None (which often is something that > is being checked first) then it would be simpler, faster, and more readable > to use > > if str(s) == 'some string': do_something() > > John, do you see any problems with this? I think str(s) is guaranteed to > return a string--that is, not to fail--for any python object, correct?
My guess is that your code will work, but I am disinclined to remove a check that is working and was probably added to satisfy some corner case we are forgetting about. str can fail, BTW: In [35]: class Evil: ....: def __str__(self): ....: raise NotImplementedError ....: In [36]: evil = Evil() In [37]: print str(evil) ------------------------------------------------------------ Traceback (most recent call last): File "<ipython console>", line 1, in ? File "<ipython console>", line 3, in __str__ NotImplementedError ------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/ _______________________________________________ Matplotlib-devel mailing list Matplotlib-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-devel