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

John Nagle nagle at
Sun Aug 22 07:40:07 CEST 2010

On 8/21/2010 1:21 PM, Vlastimil Brom wrote:
> 2010/8/21<python at>:
>> 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.

    Bad idea.  Use a dict; don't try to pretend that an object is a dict.
This isn't Javascript.  Incidentally, inheriting from "dict" works,
and is quite useful.

	class item(dict) :

         p = item()
         p['abc'] = 1

That wasn't in early versions of Python, which led to a style of abusing
objects as if they were dictionaries.

    Also note that 1) spaces in attribute names can be troublesome, and
2) duplicating the name of a function or built-in attribute will 
override it, usually leading to unwanted results.

					John Nagle

More information about the Python-list mailing list