all Python 3inspectfunction
inspect.isdatadescriptor()
descriptors
Checks that an object is a data descriptor: it has __set__ or __delete__ (property, __slots__ entries, attributes of C types).
Syntax
inspect.isdatadescriptor(object) -> bool
The difference from a method descriptor is priority: a data descriptor in the class beats the instance __dict__, so a property cannot be shadowed by obj.p = 1; the setter runs or you get AttributeError. A plain method is not a data descriptor: assigning a.m = ... just hides it behind an instance attribute. Check the object from the class (A.__dict__['p']), not the value read through an instance.
Examples
import inspect
class A:
@property
def p(self):
return 1
def m(self):
return 2
print(inspect.isdatadescriptor(A.__dict__['p'])) # → True
print(inspect.isdatadescriptor(A.__dict__['m'])) # → False
a = A()
a.m = 'спрятан'
print(a.m) # → спрятан
try:
a.p = 5
except AttributeError:
print('property не перекрыть') # → property не перекрыть
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy