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

Discussion in 'Python' started by mrdylan, Apr 12, 2006.

  1. mrdylan

    mrdylan Guest

    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!
    mrdylan, Apr 12, 2006
    #1
    1. Advertising

  2. 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
    Ben Cartwright, Apr 12, 2006
    #2
    1. Advertising

  3. 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.
    Steven D'Aprano, Apr 12, 2006
    #3
  4. 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.
    Steven D'Aprano, Apr 12, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sridhar R
    Replies:
    14
    Views:
    1,380
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  2. Robert Oschler
    Replies:
    3
    Views:
    465
    Christopher T King
    Jul 27, 2004
  3. Gerard Flanagan
    Replies:
    3
    Views:
    427
    Terry Hancock
    Nov 19, 2005
  4. Martin P. Hellwig
    Replies:
    1
    Views:
    362
    Martin P. Hellwig
    Mar 26, 2010
  5. Zarko Gajic

    Custom Attributes (AttributeTargets.Property) and Property Type

    Zarko Gajic, Apr 7, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    423
    Zarko Gajic
    Apr 7, 2005
Loading...

Share This Page