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

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Apr 12 01:19:17 EDT 2006


On Tue, 11 Apr 2006 19:11:46 -0700, mrdylan wrote:

> Given a class like so:
> 
> -------------------------------
> 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 calls t.get() which returns None. So t.p is None, and type(t.p) is
NoneType.


> 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.

If you want a general purpose solution for an object introspection tool,
I must admit I'm as lost as you are. The only thing I have come up with is
this nasty hack:

>>> t.p is None
True
>>> dir(t.p) is dir(None) 
False
>>> for attr in dir(None):
...     print attr, getattr(None, attr) is getattr(t.p, attr)
...
__class__ True
__delattr__ False
__doc__ True
__getattribute__ False
__hash__ False
__init__ False
__new__ True
__reduce__ False
__reduce_ex__ False
__repr__ False
__setattr__ False
__str__ False

I don't even know if this hack generalises to any values of p, and I
suspect it isn't of practical use. 

Why do you want to know if p is a property?


-- 
Steven.




More information about the Python-list mailing list