473,591 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

To get all property of a class?

Exist a sintax like

import Foo
for property in Foo
print property

?
My problem is to print all property values of various class,
without know the number and the type of properties.

Example. Print all property of class circle:
NAME = Foo
RADIUS = 10
SEGMENT = 15
FILL = 0
etc...

extract from

self.name = "Foo"
self.radius = 10
self.segment = 15
etc..

Thanks,

Manuel Bastioni

Jul 18 '05 #1
6 11551
manuel wrote:
Exist a sintax like

import Foo
for property in Foo
print property


Yes, use __dict__, it contains the mapping of all attributes
of an object (module, class, whatever) to their values:

import os
for (name,value) in os.__dict__:
print name,"=",value
--Irmen

Jul 18 '05 #2
> Exist a sintax like

import Foo
for property in Foo
print property

for i in dir(a):
if a[0] <>'_':
print a;'=';a.i # isn't work, I don't know how to manage it - 'a.i'
dir( [object])

Without arguments, return the list of names in the current local symbol
table. With an argument, attempts to return a list of valid attributes for
that object. This information is gleaned from the object's __dict__
attribute, if defined, and from the class or type object. The list is not
necessarily complete. If the object is a module object, the list contains
the names of the module's attributes. If the object is a type or class
object, the list contains the names of its attributes, and recursively of
the attributes of its bases. Otherwise, the list contains the object's
attributes' names, the names of its class's attributes, and recursively of
the attributes of its class's base classes. The resulting list is sorted
alphabetically. For example:
import struct
dir() ['__builtins__', '__doc__', '__name__', 'struct'] dir(struct)

['__doc__', '__name__', 'calcsize', 'error', 'pack', 'unpack']

Note: Because dir() is supplied primarily as a convenience for use at an
interactive prompt, it tries to supply an interesting set of names more than
it tries to supply a rigorously or consistently defined set of names, and
its detailed behavior may change across releases.

Jul 18 '05 #3
> import os
for (name,value) in os.__dict__:
print name,"=",value


it don't work (I'm using python into Blender distribution,
no full python installed).

I try with various modules, like

import Blender
for (name,value) in Blender.__dict_ _:
print name,"=",value

This is the error message:
Traceback (most recent call last):
File "Text.003", line 2, in ?
ValueError: too many values to unpack

The method of Egor don't work too. I've tried
with a list of lamp objects (instances of povlamp class):

for lamp in lamps:
for i in dir(lamp):
if i[0] != "_":
print lamp.i

Result:
AttributeError: PovLamp instance has no attribute 'i'

Jul 18 '05 #4
manuel wrote:
import os
for (name,value) in os.__dict__:
print name,"=",value
This should have been:
import os
for key,value in os.__dict__.ite ms():
.... print key,repr(value)
....

....
The method of Egor don't work too. I've tried
with a list of lamp objects (instances of povlamp class):

for lamp in lamps:
for i in dir(lamp):
if i[0] != "_":
print lamp.i

That last line should be:
print getattr(lamp, i )

that is, you want to use the string value stored in i to get the
attribute, not get an attribute named 'i'.

HTH,
Mike

_______________ _______________ _________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/


Jul 18 '05 #5
This should have been:
>>> import os
>>> for key,value in os.__dict__.ite ms():

... print key,repr(value)
for lamp in lamps:
for i in dir(lamp):
if i[0] != "_":
print lamp.i

That last line should be:
print getattr(lamp, i )

that is, you want to use the string value stored in i to get the
attribute, not get an attribute named 'i'.

HTH,
Mike


THANKS!
Now both work fine! In your opinion, what's the best
method ?

Best regards,
Manuel Bastioni
Jul 18 '05 #6
Mike C. Fletcher wrote:
manuel wrote:
import os
for (name,value) in os.__dict__:
print name,"=",value

It was me who wrote that, actually.
This should have been:
>>> import os
>>> for key,value in os.__dict__.ite ms():

... print key,repr(value)
...


And I should test my code before I post, thank you :-)

--Irmen

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2478
by: Johnny M | last post by:
using Access 2003 Pardon the subject line, but I don't have a better word for this strange behavior (or behavior I don't understand!!!) I have a class module named DepreciationFactor. One of the properties is a follows (irrelevant code omitted):
3
1772
by: Erik Harris | last post by:
I apologize if this is a stupid question - I'm relatively new to OOP. I have a property that must exist in a class in order to be used by another class. The property, however, does not change with each instance (it returns an instance of a delegate that points to the same method no matter what the instance). I thought the best way to make sure that Class1 could be used by Class2 would be to create an interface that defined this property,...
7
6606
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
0
5557
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 is used to view the state of the running jobs and schedule new jobs. The control also runs in the context of Internet Explorer (we do this so the administrators of the jobs can always receive the latest control). The property grid is used to...
3
6738
by: Marty McFly | last post by:
Hello, I have a control class that inherits from System.Web.UI.WebControls.Button. When I drag this control from the "My User Controls" tab in the toolbox onto the form, I want it to reflect the following default properties: Height = 32px, Width = 144px. I declare the Width property in my control as... \\\
0
1444
by: Jordan Bowness | last post by:
I make a similar post in another newsgroup, but this example is simplified somewhat. I have a component (cmpMyComponent) with 2 properties. The 1st property is a string value (Description) and the 2nd property is a strongly typed collection class (myCollectionProperty). The collection contains a simple class (myCustomClass) which has 1 text property (TextProperty).
2
3196
by: miben | last post by:
I need to set a variable returned by a readonly property in a class by another class. So the only way to set that value is from a specific class and function. Public Sub Main Dim setter As New GoingToSetYou Dim item As New ToBeSet setter.NewItem(item) Dim sValue As String = item.Value
2
2197
by: garyusenet | last post by:
I could do with something similiar, can you tell me if you think this would work for me, and if there's any advantage in working with controls this way than how I currently am. At the moment i'm using the treenodes and each treenode needs a unique entry into my rich text box. After sitting at home with MSDN i've managed to get this functionality by storing a RTF string in the tag property of the treenode. On every 'before update' of the...
8
2012
by: Al | last post by:
I'd like to create Class Library in VB 2005, which has a property accessible by external programs. I decided to include 1 Class with 1 property in this project. I placed this code in Class: Public Class COM Private mMyProp As String Public Property MyProp() As String
14
2402
by: Dom | last post by:
Hi all I'm developing a control, and I need to hide some properties to the user. For example, suppose I need Text property to be completely inacessible (from a Form/Code that is into another project/assembly). I tried with attributes: <Browsable(False), _ EditorBrowsable(EditorBrowsableState.Never), _ RefreshProperties(RefreshProperties.Repaint), _
0
7934
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8236
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8362
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7992
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8225
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5400
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3850
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3891
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1465
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.