Python Forum
iterate over class properties?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
iterate over class properties?
#2
(Aug-26-2018, 10:44 AM)NobahdiAtoll Wrote: is there a way to iterate over only properties instead of __dict__ attributes
There are ways of course, I give you a solution below. Before I do that, let me say that it is not a good way to handle your problem because properties are an implementation detail of your code and this should not be mixed with the problem's real world model. I mean that the list of items that you want to output should not be defined by the details of the implementation.

That said, the code below defines a function add_property_names() which adds a property_names() class method to a class and its subclasses. This method allows you to list the names of the class' properties.
In your example, you would add the code
add_property_names(videofile)
Then you could do
for name in movieitem.property_names():
    print(name, getattr(movieitem, name))
Here is the code
import inspect

def add_property_names(cls):
    def get_names(cls):
        for tp in inspect.getmro(cls):
            for name, value in vars(tp).items():
                if isinstance(value, property):
                    yield name
    def property_names(cls):
        register =  add_property_names.register
        if not cls in register:
            register[cls] = sorted(set(get_names(cls)))
        return register[cls]
    cls.property_names = classmethod(property_names)
    return cls
add_property_names.register = {}

if __name__ == '__main__':
    class A:
        pass

    class B(A):
        @property
        def foo(self):
            return 0
        
    class C(B):
        @property
        def bar(self):
            return self._bar
        
        @bar.setter
        def bar(self, value):
            self._bar = value

    add_property_names(A)

    a = A()
    print(a.property_names())
    c = C()
    print(c.property_names())
    print(C.property_names())
Reply


Messages In This Thread
iterate over class properties? - by NobahdiAtoll - Aug-26-2018, 10:44 AM
RE: iterate over class properties? - by Gribouillis - Aug-26-2018, 03:21 PM
RE: iterate over class properties? - by Gribouillis - Aug-26-2018, 07:02 PM
RE: iterate over class properties? - by Gribouillis - Aug-26-2018, 07:26 PM
RE: iterate over class properties? - by Gribouillis - Aug-26-2018, 07:46 PM
RE: iterate over class properties? - by Gribouillis - Aug-26-2018, 08:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PDF properties doesn't show created or modified date Pedroski55 4 1,174 Jun-19-2023, 08:09 AM
Last Post: Pedroski55
  How do I list properties quarinteen 0 1,086 May-01-2022, 04:15 PM
Last Post: quarinteen
  API design question: use methods or properties? johsmi96 1 1,720 Oct-12-2020, 02:24 PM
Last Post: buran
  Create a 3D volume with some properties. Rosendo 0 1,467 Jul-18-2020, 08:20 PM
Last Post: Rosendo
  printing class properties from numpy.ndarrays of objects pjfarley3 2 1,997 Jun-08-2020, 05:30 AM
Last Post: pjfarley3
  adding properties to variables rudihammad 0 1,713 May-06-2020, 05:09 AM
Last Post: rudihammad
  Advance properties read from xml files python1234 0 2,453 Apr-25-2018, 01:42 PM
Last Post: python1234

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020