To get all property of a class?

M

manuel

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
 
E

Egor Bolonev

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

Irmen de Jong

manuel said:
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
 
M

manuel

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'
 
M

Mike C. Fletcher

This should have been:
.... 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/
 
M

manuel

This should have been:
import os
for key,value in os.__dict__.items():
... 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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top