Iterate through slots

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Jan 21 00:14:36 EST 2008


En Tue, 15 Jan 2008 16:35:52 -0200, Jared Grubb <jared.grubb at gmail.com>  
escribi�:

> How can I iterate through the slots of a class, including those it  
> inherits
> from parent classes?
>
> class C(object):
>   __slots__ = ['a']
>
> class D(C):
>   __slots__ = ['b']
>

Iterate over all the class hierarchy keeping track of slot items:

def find_slots(cls):
   slots = set(getattr(cls, '__slots__', []))
   for base in cls.__bases__:
     slots.update(find_slots(base))
   return slots


-- 
Gabriel Genellina




More information about the Python-list mailing list