Iteration of strings
Alan Kennedy
alanmk at hotmail.com
Sun Nov 24 09:10:50 EST 2002
Bjarke Dahl Ebert wrote:
>> When writing small scripts in Python, I often make the same error:
>>
>> Some function expects a list of strings, and does something like
>> for elem in thestringlist: ...
>>
>> What do you think?
Chad Netzer wrote:
> Use an assertion in your function (or script):
>
> def dont_allow_string_argument( s ):
> assert not isinstance( s, type("") )
There is only one small problem with this, recognising unicode strings.
>>>dont_allow_string_argument("Hello World!")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in dont_allow_string_argument
AssertionError
>>>dont_allow_string_argument(u"Hello World!")
>>>
I suggest the following amendment:
import types
def dont_allow_string_argument(v):
assert type(v) not in [types.StringType, types.UnicodeType]
regards,
--
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/mailto/alan
More information about the Python-list
mailing list