reducing self.x=x; self.y=y; self.z=z boilerplate code

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

At 03:59 PM 7/1/2005 -0700, Ralf W. Grosse-Kunstleve wrote:
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 extends to any number of arguments: class grouping: def __init__(self, x, y, z): self.__dict__.update(locals()) del self.self Or if you prefer a more generic approach: def initialize(ob, args): if 'self' in args: del args['self'] for k, v in args.items(): setattr(ob,k,v) class grouping: def __init__(self, x, y, z): initialize(self, locals()) There's really no need for special syntax here, if your goal is simply to reduce boilerplate.
I'll write a PEP if I hear a few voices of support.
-1; there are lots of good solutions for this. For me, I usually have a base class with something like this: def __init__(self, **kw): for k, v in kw.items(): if not hasattr(self.__class__, k): raise TypeError("%s has no %r attribute" % (self.__class__,k)) else: setattr(self,k,v) And then subclasses define their attributes and defaults using class attributes, properties, or other descriptors.
(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).
There are more efficient solutions, especially __dict__.update().

On Fri, 01 Jul 2005 19:22:20 -0400, "Phillip J. Eby" <pje@telecommunity.com> wrote:
At 03:59 PM 7/1/2005 -0700, Ralf W. Grosse-Kunstleve wrote: [snip]
This extends to any number of arguments:
class grouping: def __init__(self, x, y, z): self.__dict__.update(locals()) del self.self
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). 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. Jp

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).
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.
That's where PJE's more generic approach comes in: def initialize(ob, args, excluded=['self']): for k in excluded: if k in args: del args[k] for k, v in args.items(): setattr(ob,k,v) class grouping: def __init__(self, x, y, z): initialize(self, locals()) Or, one could have a look at the 'namespace' module, which came out of the last pre-PEP covering this kind of area: http://namespace.python-hosting.com/ 'Record' is particularly interesting from an auto-initialisation point of view (the class attributes define the expected instance attributes). Although I may be a little biased, since I wrote that class. . . Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia --------------------------------------------------------------- http://boredomandlaziness.blogspot.com

Nick Coghlan <ncoghlan@gmail.com> wrote:
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).
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.
That's where PJE's more generic approach comes in:
def initialize(ob, args, excluded=['self']): for k in excluded: if k in args: del args[k] for k, v in args.items(): setattr(ob,k,v)
class grouping: def __init__(self, x, y, z): initialize(self, locals())
I'm with everyone else on this, -1 on .x syntax. As provided in the 6 line function above, everything desired is available. You want something that you don't need to use the excluded argument for, but still has the same stench as what Ralf originally offered? def initialize(ob, args): for k, v in args.items(): if k[:1] == '_': setattr(ob,k[1:],v) class grouping: def __init__(self, _x, _y, _z): initialize(self, locals()) Now, don't get me wrong, definining __slots__ can be a pain in the tookus, but with a proper metaclass, that metaclass can define the __slots__ attribute based on the argument list for __init__(). There you go. A syntax change is wholly unnecessary. - Josiah

On Fri, Jul 01, 2005, Ralf W. Grosse-Kunstleve wrote:
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 off-topic for python-dev. Please take it to comp.lang.python. (It's not immediately obvious that this is off-topic, I know, but please take my word for it.) -- Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/ f u cn rd ths, u cn gt a gd jb n nx prgrmmng.

On Saturday 02 July 2005 08:59, Ralf W. Grosse-Kunstleve wrote:
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
-1. Syntax should not look like grit on my monitor. Anthony -- Anthony Baxter <anthony@interlink.com.au> It's never too late to have a happy childhood.
participants (7)
-
Aahz
-
Anthony Baxter
-
Josiah Carlson
-
Jp Calderone
-
Nick Coghlan
-
Phillip J. Eby
-
Ralf W. Grosse-Kunstleve