Checking whether type is None

Schachner, Joseph Joseph.Schachner at Teledyne.com
Wed Jul 25 12:14:18 EDT 2018


While I appreciate that use of "is" in   thing is None, I claim this relies on knowledge of how Python works internally, to know that every None actually is the same ID (the same object) - it is singular.  That probably works for 0 and 1 also but you probably wouldn't consider testing   thing is 1, at least I hope you wouldn't.  thing is None looks just as odd to me.  Why not thing == None ?  That works.

--- Joseph S.

-----Original Message-----
From: Tobiah <toby at tobiah.org> 
Sent: Tuesday, July 24, 2018 3:33 PM
To: python-list at python.org
Subject: Checking whether type is None

Consider:

	>>> type({}) is dict
	True
	>>> type(3) is int
	True
	>>> type(None) is None
	False

Obvious I guess, since the type object is not None.
So what would I compare type(None) to?

	>>> type(None)
	<type 'NoneType'>
	>>> type(None) is NoneType
	Traceback (most recent call last):
	  File "<stdin>", line 1, in <module>
	NameError: name 'NoneType' is not defined


I know I ask whether:

	>>> thing is None

but I wanted a generic test.
I'm trying to get away from things like:

	>>> type(thing) is type(None)

because of something I read somewhere preferring my original test method.


Thanks



More information about the Python-list mailing list