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

2006-04-11 Thread Ben Cartwright
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 XXX
 ---

 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

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2006-04-11 Thread Steven D'Aprano
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 XXX
 ---
 
 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.

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2006-04-11 Thread Steven D'Aprano
Replying to myself now... how sad.

On Wed, 12 Apr 2006 15:19:17 +1000, Steven D'Aprano wrote:

 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

Scrub that.

 dir(t.p) is dir(t.p)
False
 dir(None) is dir(None)
False


Which in hindsight is so obvious that I'll just slink off in embarrassment
for having made the error in the first place.


-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list