How can I determine the property attributes on a class or instance?

Ben Cartwright bencvt at gmail.com
Tue Apr 11 22:23:48 EDT 2006


mrdylan wrote:
> class TestMe(object):
>   def get(self):
>     pass
>   def set(self, v):
>     pass
>
>   p = property( get, set )
>
> t = TestMe()
> type(t.p)         #returns NoneType, what???
> t.p.__str__      #returns <method-wrapper object at XXXXXXX>
> -----------------------------------
>
> What is the best way to determine that the attribute t.p is actually a
> property object? Obviously I can test the __str__ or __repr__
> attributes using substring comparison but there must be a more elegant
> idiom.

Check the class instead of the instance:

  >>> type(TestMe.p)
  <type 'property'>
  >>> type(t.__class__.p)
  <type 'property'>
  >>> isinstance(t.__class__.p, property)
  True

--Ben




More information about the Python-list mailing list