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é 5 1653
On 2007-06-25, André <an***********@gmail.comwrote:
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 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__
In <ma*************************************@python.or g>, 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
En Mon, 25 Jun 2007 15:10:25 -0300, Marc 'BlackJack' Rintsch
<bj****@gmx.netescribió:
In <ma*************************************@python.or g>, 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
On Jun 25, 2:09 pm, Neil Cerutti <horp...@yahoo.comwrote:
On 2007-06-25, André <andre.robe...@gmail.comwrote:
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: sneill |
last post by:
How is it possible to take the value of a variable (in this case,
MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name?
In the following example I want 'oIcon' object to have...
|
by: MP |
last post by:
Hello
I want to have a public property (and Brows able) in one control, I use this
code:
public System.Windows.Forms.Form recordForm
get { return _recordForm;}
set {_recordForm = value;}
|
by: Vicky via DotNetMonster.com |
last post by:
Hi, I need help with "An object reference is required for the nonstatic
field, method, or property 'dataReader.Class1.data'"
Before I put folowing variable in class level, it works fine....
|
by: critter |
last post by:
I am trying to get a value of a property of a class, but not an
instance of the class. Using Reflection, I can find the class in my
assembly, and I can find the property of my class, however, I...
|
by: Brian Young |
last post by:
Hi all.
I'm using the Property Grid control in a control to manage a windows service
we have developed here. The windows service runs a set of other jobs that
need to be managed. The control...
|
by: Lüpher Cypher |
last post by:
Hi,
Suppose we have a hierarchical class structure that looks something like
this:
Object
|
+-- Main
|
+-- Object1
|
by: Joergen Bech |
last post by:
(Slightly religious question):
Suppose I have the following class:
---snip---
Public Class MyClass
Private _MyVariable As Integer
Public Property MyVariable() As Integer
Get
|
by: TS |
last post by:
is it preferred to access member variables directly in code, on the page
that declared them, versus going thru a property accessor? I would think
that since theres no security concerns or anything...
|
by: =?Utf-8?B?UGhpbGw=?= |
last post by:
I am making the transition from VB to C# and am stumped on a seemingly simple
task. I have created a class and defined a property using set/get. Now from
one of my forms I want to set the...
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |