listing all property variables of a class instance

Discussion in 'Python' started by =?iso-8859-1?B?QW5kcuk=?=, Jun 25, 2007.

  1. Suppose I define a class with a number of variables defined as
    properties. Something like:

    class MyClass(object):

    def __init__(self):
    self.some_variable = 42
    self._a = None
    self._b = "pi"

    def get_a(self):
    return self._a
    def set_a(self, value):
    self._a = value

    def get_b(self):
    return self._b
    def set_b(self, value):
    self._b = value

    a = property(get_a, set_a, None, "a is a property")
    b = property(get_b, set_b, None, "b is a property")

    Is there a way to write a method that would list automatically all the
    variables defined as a property (say by printing their docstring and/
    or their value), and only those variables?

    André
     
    =?iso-8859-1?B?QW5kcuk=?=, Jun 25, 2007
    #1
    1. Advertising

  2. =?iso-8859-1?B?QW5kcuk=?=

    Neil Cerutti Guest

    On 2007-06-25, André <> wrote:
    > Suppose I define a class with a number of variables defined as
    > properties. Something like:
    >
    > class MyClass(object):
    >
    > def __init__(self):
    > self.some_variable = 42
    > self._a = None
    > self._b = "pi"
    >
    > def get_a(self):
    > return self._a
    > def set_a(self, value):
    > self._a = value
    >
    > def get_b(self):
    > return self._b
    > def set_b(self, value):
    > self._b = value
    >
    > a = property(get_a, set_a, None, "a is a property")
    > b = property(get_b, set_b, None, "b is a property")
    >
    > Is there a way to write a method that would list automatically
    > all the variables defined as a property (say by printing their
    > docstring and/ or their value), and only those variables?


    This is off the cuff. There's likely a better way.

    for k, v in MyClass.__dict__.iteritems():
    if isinstance(v, property):
    print k, v.__doc__

    --
    Neil Cerutti
    22 members were present at the church meeting held at the home of Mrs. Marsha
    Crutchfield last evening. Mrs. Crutchfield and Mrs. Rankin sang a duet, The
    Lord Knows Why. --Church Bulletin Blooper
     
    Neil Cerutti, Jun 25, 2007
    #2
    1. Advertising

  3. =?iso-8859-1?B?QW5kcuk=?=

    Jay Loden Guest

    Neil Cerutti wrote:
    >> Is there a way to write a method that would list automatically
    >> all the variables defined as a property (say by printing their
    >> docstring and/ or their value), and only those variables?

    >
    > This is off the cuff. There's likely a better way.
    >
    > for k, v in MyClass.__dict__.iteritems():
    > if isinstance(v, property):
    > print k, v.__doc__
    >


    The only way I could get this to work was to change the way the properties were defined/initalized:

    #!/usr/bin/python

    class MyClass(object):

    def __init__(self):
    self.some_variable = 42
    self._a = None
    self._b = "pi"
    self.a = property(self.get_a, self.set_a, None, "a is a property")
    self.b = property(self.get_b, self.set_b, None, "b is a property")

    def get_a(self):
    return self._a
    def set_a(self, value):
    self._a = value

    def get_b(self):
    return self._b
    def set_b(self, value):
    self._b = value


    test = MyClass()
    for k,v in test.__dict__.iteritems():
    if isinstance(v, property):
    print k, v.__doc__
     
    Jay Loden, Jun 25, 2007
    #3
  4. In <>, Jay Loden wrote:

    >
    > Neil Cerutti wrote:
    >>> Is there a way to write a method that would list automatically
    >>> all the variables defined as a property (say by printing their
    >>> docstring and/ or their value), and only those variables?

    >>
    >> This is off the cuff. There's likely a better way.
    >>
    >> for k, v in MyClass.__dict__.iteritems():
    >> if isinstance(v, property):
    >> print k, v.__doc__
    >>

    >
    > The only way I could get this to work was to change the way the
    > properties were defined/initalized:


    That's because you iterate over the instance's `__dict__` and not over the
    *class* `__dict__` like Neil does.

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Jun 25, 2007
    #4
  5. En Mon, 25 Jun 2007 15:10:25 -0300, Marc 'BlackJack' Rintsch
    <> escribió:

    > In <>, Jay Loden wrote:
    >> Neil Cerutti wrote:
    >>>> Is there a way to write a method that would list automatically
    >>>> all the variables defined as a property (say by printing their
    >>>> docstring and/ or their value), and only those variables?
    >>>
    >>> This is off the cuff. There's likely a better way.
    >>>
    >>> for k, v in MyClass.__dict__.iteritems():
    >>> if isinstance(v, property):
    >>> print k, v.__doc__
    >>>

    >>
    >> The only way I could get this to work was to change the way the
    >> properties were defined/initalized:

    >
    > That's because you iterate over the instance's `__dict__` and not over
    > the
    > *class* `__dict__` like Neil does.


    I would iterate over dir(MyClass) instead - only because I prefer to hide
    such implementation details.

    --
    Gabriel Genellina
     
    Gabriel Genellina, Jun 25, 2007
    #5
  6. On Jun 25, 2:09 pm, Neil Cerutti <> wrote:
    > On 2007-06-25, André <> wrote:
    >
    >
    >
    > > Suppose I define a class with a number of variables defined as
    > > properties. Something like:

    >
    > > class MyClass(object):

    >
    > > def __init__(self):
    > > self.some_variable = 42
    > > self._a = None
    > > self._b = "pi"

    >
    > > def get_a(self):
    > > return self._a
    > > def set_a(self, value):
    > > self._a = value

    >
    > > def get_b(self):
    > > return self._b
    > > def set_b(self, value):
    > > self._b = value

    >
    > > a = property(get_a, set_a, None, "a is a property")
    > > b = property(get_b, set_b, None, "b is a property")

    >
    > > Is there a way to write a method that would list automatically
    > > all the variables defined as a property (say by printing their
    > > docstring and/ or their value), and only those variables?

    >
    > This is off the cuff. There's likely a better way.
    >
    > for k, v in MyClass.__dict__.iteritems():
    > if isinstance(v, property):
    > print k, v.__doc__


    Thank you, this solved my problem nicely.

    André

    >
    > --
    > Neil Cerutti
    > 22 members were present at the church meeting held at the home of Mrs. Marsha
    > Crutchfield last evening. Mrs. Crutchfield and Mrs. Rankin sang a duet, The
    > Lord Knows Why. --Church Bulletin Blooper
     
    =?iso-8859-1?B?QW5kcuk=?=, Jun 25, 2007
    #6
    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. Replies:
    10
    Views:
    35,906
    jporter892
    Jun 6, 2011
  2. Replies:
    4
    Views:
    276
    Jaspreet
    Jun 23, 2005
  3. Eric D.
    Replies:
    3
    Views:
    180
    Jeremy Henty
    Feb 1, 2006
  4. Ralph Shnelvar
    Replies:
    29
    Views:
    781
    David Masover
    Nov 30, 2009
  5. yawnmoth
    Replies:
    12
    Views:
    289
    yawnmoth
    Feb 1, 2009
Loading...

Share This Page