You could use inspect.isdatadescriptor
:
Return true if the object is a data descriptor. ... Examples are
properties (defined in Python), getsets, and members.
...
CPython implementation detail: getsets are attributes defined in extension modules via PyGetSetDef structures.
...
CPython implementation detail: Member descriptors are attributes defined in extension modules via PyMemberDef structures
Data descriptors are just types that have certain methods. See 3.3.2.1. Implementing Descriptors:
If the descriptor defines __set__()
and/or __delete__()
, it is a data
descriptor; if it defines neither, it is a non-data descriptor.
Non-data descriptors include classmethod
and staticmethod
(that is, they're not functions, they're types). For example, inspect.isdatadescriptor(MyClass.this_is_a_classmethod)
would return False
.
On the other hand, property
is a data descriptor:
In [6]: inspect.isdatadescriptor(MyClass.this_is_a_property)
Out[6]: True
The downside of using this function is that it may return True
if isinstance(mystery, property)
is False
.
A better way is to check for object type directly:
In [7]: isinstance(MyClass.this_is_a_property, property)
Out[7]: True