Python Forum
iterate over class properties?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
iterate over class properties?
#8
The most important call is add_property_names(A) if you look at what it does in line 18, it adds a new classmethod named property_names to class A. Everything happens as if we had written
class A:
    @classmethod
    def property_names(cls):
        ...
It means that we can now call A.property_names() or A().property_names() or even C.property_names() because C is a subclass of A.

Now look at this method at line 11. It looks in a global dictionary _register
if we have already stored a list of property names _register[A] for class A. If not it computes this list by calling _get_names(A). This way the list of properties is only computed once for each class.

Last subtlety: add_property_names(cls) returns cls. This allows us to use it as a class decorator and write
@add_property_names
class A:
    pass
so that we don't need to call explicitly add_property_names after the definition of class A.
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,087 May-01-2022, 04:15 PM
Last Post: quarinteen
  API design question: use methods or properties? johsmi96 1 1,723 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,714 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