I'm looking for a way to reliably get a list of all slot names for a class with __slots__. Example:
class A:
__slots__ = ('a',)
print(list_slots(A)) # should output ['a']
Of course, this can be done with list(A.__slots__), but unfortunately there are a couple of things that make this more difficult:
Inherited slots:
class B(A): __slots__ = ('b',) print(list_slots(B)) # should output ['a', 'b']Strings:
class C: __slots__ = 'xyz' print(list_slots(C)) # should output ['xyz']Iterators:
class D: __slots__ = iter(['d']) print(list_slots(D)) # should output ['d']
As you can see, there are plenty of pitfalls here... is there an implementation of list_slots that handles all of these cases correctly?