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

M

mrdylan

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

thanks!
 
B

Ben Cartwright

mrdylan said:
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:
True

--Ben
 
S

Steven D'Aprano

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:
.... 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?
 
S

Steven D'Aprano

Replying to myself now... how sad.

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:

False

Scrub that.
False


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top