import string
print string.ascii_lowercase # abcdefghijklmnopqrstuvwxyz
print type(string.ascii_lowercase) # <type 'str'>
print string.ascii_lowercase is str # False
Shouldn't it be True?
import string
print string.ascii_lowercase # abcdefghijklmnopqrstuvwxyz
print type(string.ascii_lowercase) # <type 'str'>
print string.ascii_lowercase is str # False
Shouldn't it be True?
The is operator compares the identity of two objects. This is what I believe it does behind the scenes:
id(string.ascii_lowercase) == id(str)
Actual strings are always going to have a different identity than the type str, so this will always be False.
Here is the most Pythonic way to test whether something is a string:
isinstance(string.ascii_lowercase, basestring)
This will match both str and unicode strings.
use:
>>> isinstance('dfab', str)
True
is intended for identity testing.
string.ascii_lowercase is str should not be True.
type(string.ascii_lowercase) is str is True.
The is keyword checks object identity, not type.
You may have seen code like foo is None often and thought that None is a type. None is actually a singleton object.
string.ascii_lowercase is a string, whereas str is a type.is test's whether they are the same object. i.e. they are both pointers to an object, are those objects the same.type(str)