Iterate through class members in order of their declaration

后端 未结 3 678
南旧
南旧 2020-12-15 08:51

I got this problem writing a little GUI lib that maps classes to simple table views. Every class member of a certain type = column, and order of columns is important. But...

3条回答
  •  死守一世寂寞
    2020-12-15 09:22

    Ok, it's not an answer per se but a workaround that only works in the context of original question ("Every class member [which is an instance] of a certain type = column, and order of columns is important"). The solution is to introduce a class variable _count into the CertainType class, and increment it at every instantiation. After that all class members of the CertainType are packed into a list which is sorted using key=attrgetter('_count')

    P.S. Omitting that "which is an instance" part was a mistake on my part and it has limited range of solutions considerably. Sorry for that.

提交回复
热议问题