Iteration of strings
Alex Martelli
aleax at aleax.it
Mon Nov 25 06:57:53 EST 2002
Duncan Booth wrote:
...
> Of course that still doesn't catch the case where someone subclassed a
> string type. If you are sufficiently paranoid you might consider:
>
> def dont_allow_string_argument(v):
> for t in types.StringTypes:
> assert not isinstance(v, t)
loop unneeded in recent Python versions (at least since 2.2.1 -- no older
one around to check, sorry):
>>> import types
>>> isinstance('ciao', types.StringTypes)
1
>>> isinstance(23, types.StringTypes)
0
>>>
Or, you could catch just about all "string workalikes" with:
def raise_on_stringy_types(v):
try: v+''
except: pass
else: raise TypeError, "Type %s not acceptable" % type(v)
Alex
More information about the Python-list
mailing list