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? Eric ------------------------------------------------------------------------- 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