Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iterate through __slots__
#2
Mro is the way to go. Use
def all_slots(ob):
    if not hasattr(ob, 'mro'):
        ob = type(ob)
    for cls in reversed(ob.mro()):
        yield from cls.__dict__.get('__slots__', ())
and replace for attr in self.__slots__ with for attr in all_slots(self)

Edit: bugfix: changed getattr to __dict__.get to avoid repetition in case subclasses don't have slots.
Reply


Messages In This Thread
Iterate through __slots__ - by deanhystad - Mar-08-2020, 05:46 AM
RE: Iterate through __slots__ - by Gribouillis - Mar-08-2020, 02:57 PM
RE: Iterate through __slots__ - by deanhystad - Mar-08-2020, 08:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  question about __slots__ akbarza 1 524 Dec-17-2023, 08:02 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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