To get all property of a class?

Discussion in 'Python' started by manuel, Jun 29, 2003.

  1. manuel

    manuel Guest

    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
     
    manuel, Jun 29, 2003
    #1
    1. Advertising

  2. manuel

    Egor Bolonev Guest

    > 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.
     
    Egor Bolonev, Jun 30, 2003
    #2
    1. Advertising

  3. 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
     
    Irmen de Jong, Jun 30, 2003
    #3
  4. manuel

    manuel Guest

    > 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'
     
    manuel, Jun 30, 2003
    #4
  5. 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__.items():

    .... 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/
     
    Mike C. Fletcher, Jun 30, 2003
    #5
  6. manuel

    manuel Guest


    > 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
     
    manuel, Jun 30, 2003
    #6
  7. 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__.items():

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


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

    --Irmen
     
    Irmen de Jong, Jun 30, 2003
    #7
    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. shoa

    get property of a class

    shoa, Jul 12, 2005, in forum: Java
    Replies:
    5
    Views:
    352
    Alan Krueger
    Jul 13, 2005
  2. E11
    Replies:
    1
    Views:
    4,900
    Thomas Weidenfeller
    Oct 12, 2005
  3. =?iso-8859-1?B?QW5kcuk=?=

    listing all property variables of a class instance

    =?iso-8859-1?B?QW5kcuk=?=, Jun 25, 2007, in forum: Python
    Replies:
    5
    Views:
    298
    =?iso-8859-1?B?QW5kcuk=?=
    Jun 25, 2007
  4. SAN CAZIANO
    Replies:
    1
    Views:
    265
  5. Spam Catcher
    Replies:
    4
    Views:
    166
    Darko
    Nov 21, 2007
Loading...

Share This Page