Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One very important thing to be aware of -- if those attributes stay in the class, the <code>__slots__</code> generation will be useless... okay, maybe not <em>useless</em> -- it will make the class attributes read-only; probably not what you want.</p> <p>The easy way is to say, "Okay, I'll initialize them to None, then let them disappear." Excellent! Here's one way to do that:</p> <pre><code>class B(object): three = None four = None temp = vars() # get the local namespace as a dict() __slots__ = temp.keys() # put their names into __slots__ __slots__.remove('temp') # remove non-__slots__ names __slots__.remove('__module__') # now remove the names from the local for name in __slots__: # namespace so we don't get read-only del temp[name] # class attributes del temp # and get rid of temp </code></pre> <p>If you want to keep those initial values it takes a bit more work... here's one possible solution:</p> <pre><code>class B(object): three = 3 four = 4 def __init__(self): for key, value in self.__init__.defaults.items(): setattr(self, key, value) temp = vars() __slots__ = temp.keys() __slots__.remove('temp') __slots__.remove('__module__') __slots__.remove('__init__') __init__.defaults = dict() for name in __slots__: __init__.defaults[name] = temp[name] del temp[name] del temp </code></pre> <p>As you can see, it is possible to do this without a metaclass -- but who wants all that boilerplate? A metaclass could definitely help us clean this up:</p> <pre><code>class MakeSlots(type): def __new__(cls, name, bases, attrs): new_attrs = {} new_attrs['__slots__'] = slots = attrs.keys() slots.remove('__module__') slots.remove('__metaclass__') new_attrs['__weakref__'] = None new_attrs['__init__'] = init = new_init init.defaults = dict() for name in slots: init.defaults[name] = attrs[name] return super(MakeSlots, cls).__new__(cls, name, bases, new_attrs) def new_init(self): for key, value in self.__init__.defaults.items(): setattr(self, key, value) class A(object): __metaclass__ = MakeSlots one = 1 two = 2 class B(object): __metaclass__ = MakeSlots three = 3 four = 4 </code></pre> <p>Now all the tediousness is kept in the metaclass, and the actual class is easy to read and (hopefully!) understand.</p> <p>If you need to have anything else in these classes besides attributes I strongly suggest you put whatever it is in a mixin class -- having them directly in the final class would complicate the metaclass even more.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload