Python Question

How to refer to types in Python?

I know how to refer to some types, i.e.

str
for
type('')
,
int
for
type(1)
etc. But what about other types, such as
type(lambda: None)
?

I know to refer to it as
type(f) == type(lambda: None)
for comparison, but, is there any other way, except that? (No silly answer such as code-golf, use another return value for the lambda, etc.)

Answer

If you want to test if a certain value is a lambda:

import types

foo = lambda: None
print(isinstance(foo, types.LambdaType))

See https://docs.python.org/3/library/types.html.

You usually use isinstance for testing if something is something, type() == type() is very frowned upon.

Comments