[Python-Dev] reducing self.x=x; self.y=y; self.z=z boilerplate code

Ralf W. Grosse-Kunstleve rwgk at cci.lbl.gov
Sat Jul 2 02:50:22 CEST 2005


Jp Calderone wrote:
> If you use vars(self).update(locals()), it even looks halfway
> pleasant ;)  I'm not sure what python-dev's current opinion of
> vars(obj) is though (I'm hoping someone'll tell me).

http://docs.python.org/lib/built-in-funcs.html#l2h-76 is pretty clear:

  vars([object])
    Without arguments, return a dictionary corresponding to the current
    local symbol table. With a module, class or class instance object
    as argument (or anything else that has a __dict__ attribute),
    returns a dictionary corresponding to the object's symbol table.
    The returned dictionary should not be modified: the effects on the
    corresponding symbol table are undefined.

> Of course, both of these fall over for __slots__'ful classes.  It'd be
> nice if there were a general way to deal with attributes of an
> instance, regardless of the implementation details of its memory
> layout.

I agree. Ideally I'd like this

  class grouping:

    __slots__ = True

    def __init__(self, .x, .y, .z):
      pass

to be equivalent to:

  class grouping:

    __slots__ = ["x", "y", "z"]

    def __init__(self, x, y, z):
      self.x = x
      del x
      self.y = y
      del y
      self.z = z
      del z

Cheers,
        Ralf


More information about the Python-Dev mailing list