Implicit lists
Dale Strickland-Clark
dale at riverhall.NOTHANKS.co.uk
Thu Jan 30 15:15:11 EST 2003
More information about the Python-list mailing list
Thu Jan 30 15:15:11 EST 2003
- Previous message (by thread): Implicit lists
- Next message (by thread): Implicit lists
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Alex Martelli <aleax at aleax.it> wrote: >A class is string-like if it acts like a string. If a >class lets its instances be concatenated to strings, it's >pretty string-like -- all of str, unicode and UserString >do, and no other type that's either built-in or the standard >library. Making this easily checked feature the single >discriminant of "string-likeness" appears quite OK to me >for most purposes. You can wrap the test into highly readable >form by the simple trick of defining a function;-)...e.g.: > >def isstringlike(x): > try: return x+'!' > except TypeError: return False > There's a bit of a language deficiency here if you have to resort to this type of obscure trick. To test for strings, I have always coded either of: type(var) in (str, unicode) isinstance(var, (str, unicode)) But, where it mattered, I have also been able to make sure that my string-like objects are subclasses of str. It makes you think. -- Dale Strickland-Clark Riverhall Systems Ltd
- Previous message (by thread): Implicit lists
- Next message (by thread): Implicit lists
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list