Detect string has non-ASCII chars without checking each char?

python at bdurham.com python at bdurham.com
Sat Aug 21 20:46:30 CEST 2010


Python 2.6: Is there a built-in way to check if a Unicode string
has non-ASCII chars without having to check each char in the
string?

Here's my use case: I have a section of code that makes frequent
calls to hasattr. The attribute name being tested is derived from
incoming data which at times can contain international content.

hasattr() raises an exception when passed a Unicode attribute
name. I would have expected a simple True/False return value vs.
an encoding error.

UnicodeEncodeError: 'ascii' codec can't encode character
u'\u012c' in position 0: ordinal not in range(128)

Is this behavior by design or could I be encoding the string I'm
passing hasattr() incorrectly?

If its by design, I'm thinking the best approach for me would be
to write  a hasattr_enhanced() function that traps the Unicode
encoding exception and returns False and use this function in
place of hasattr(). Any thoughts on this strategy?

Thank you,
Malcolm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100821/b566c689/attachment.html>


More information about the Python-list mailing list