<!--/*SC*/DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"/*EC*/-->
<html><head><title></title><style type="text/css"><!--body{padding:1ex;margin:0px;font-family:sans-serif;font-size:small;}a[href]{color:-moz-hyperlinktext !important;text-decoration:-moz-anchor-decoration;}blockquote{margin:0;border-left:2px solid #144fae;padding-left:1em;}blockquote blockquote{border-color:#006312;}blockquote blockquote blockquote{border-color:#540000;}--></style></head><body><div style="font-family: Arial; font-size: medium;" dir="ltr"><div>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?</div>
<div> </div>
<div>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.</div>
<div> </div>
<div>hasattr() raises an exception when passed a Unicode attribute name. I would have expected a simple True/False return value vs. an encoding error.</div>
<div> </div>
<div>UnicodeEncodeError: 'ascii' codec can't encode character u'\u012c' in position 0: ordinal not in range(128)</div>
<div> </div>
<div>Is this behavior by design or could I be encoding the string I'm passing hasattr() incorrectly?</div>
<div> </div>
<div>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?</div>
<div> </div>
<div>Thank you,</div>
<div>Malcolm</div>
<div> </div>
<br></div></body></html>