[Python-Dev] reducing self.x=x; self.y=y; self.z=z boilerplate code (original) (raw)

Ralf W. Grosse-Kunstleve rwgk at cci.lbl.gov
Sat Jul 2 00:59:46 CEST 2005


Hi,

I often find myself writing:

class grouping:

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

I hate it, and every time I show this to a Python newcomer I get that skeptic look. How about this for a change?

class grouping:

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

This is supposed to work the same way as:

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

Currently the .x syntax leads to:

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

SyntaxError: invalid syntax

I.e. it seems to me that there shouldn't be any backward compatibility issues.

I'll write a PEP if I hear a few voices of support. (Otherwise I'll stick to my "adopt_init_args" workaround: http://phenix-online.org/cctbx_sources/libtbx/libtbx/introspection.py which does a similar job but doesn't look as elegant and is also quite inefficient).

Cheers, Ralf



More information about the Python-Dev mailing list