py3k feature proposal: field auto-assignment in constructors

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun Jan 27 23:47:07 EST 2008


En Sun, 27 Jan 2008 23:51:28 -0200, Arnaud Delobelle  
<arnodel at googlemail.com> escribió:

> Nice!  I've got a slight variation without magic argument names:
>
> class Test(object):
>      @autoassign('foo', 'bar')
>      def __init__(self, baz):
>          print 'baz =', baz
>

En Mon, 28 Jan 2008 00:02:51 -0200, André <andre.roberge at gmail.com>  
escribió:

> Here's a version that
> 1. does not require new syntax
> 2. does not *necessarily* override the "_" prefix convention
> 3. follows the "Explicit is better than implicit" convention when
> being called.
>
> class Test(object):
>      @autoassign('self_')
>      def __init__(self, self_foo, self_bar, baz):
>          print 'baz =', baz

I would like a signature-preserving version. The help system, pydoc, the  
inspect module, and likely any other introspection tool see those  
decorated methods with a different signature than the original one.
Even if one writes a signature-preserving decorator (maybe along the lines  
of this article by M. Simionato [1]), names like "self_foo", "self_bar"  
are ugly. Furthermore, argument names are part of the public interfase,  
but here they're tied to the implementation: what if later I want to keep  
a reference to baz? I must change the argument name to self_baz, breaking  
all users of the code.

[1] http://www.phyast.pitt.edu/~micheles/python/documentation.html

-- 
Gabriel Genellina




More information about the Python-list mailing list