py3k feature proposal: field auto-assignment in constructors

Russ P. Russ.Paielli at gmail.com
Mon Jan 28 01:31:08 EST 2008


On Jan 27, 7:33 pm, Ben Finney <bignose+hates-s... at benfinney.id.au>
wrote:
> "Russ P." <Russ.Paie... at gmail.com> writes:
> > On Jan 27, 5:13 pm, Wildemar Wildenburger
> > <lasses_w... at klapptsowieso.net> wrote:
> > > class Server(object):
> > >      def __init__(self, self.host, self.port,
> > >                   self.protocol, self.bufsize, self.timeout):
> > >          pass
>
> > > ?
>
> > That makes sense to me.
>
> Not to me. 'self' is a name that doesn't exist until *after* that
> 'def' statement is completed; in any other statement, that would mean
> 'self.foo' in the same statement would raise a NameError.
>
> Special-casing it for a function declaration complicates the language
> for little gain: the rules of what is valid when become more
> complicated. Special cases aren't special enough to break the rules.
>
> --
>  \        "I took a course in speed waiting. Now I can wait an hour in |
>   `\                              only ten minutes."  -- Steven Wright |
> _o__)                                                                  |
> Ben Finney

OK, then how about a special function that could be called from inside
the constructor (or anywhere else for that matter) to initialize a
list of data members. For example,

self.__set__(host, port, protocol, bufsize,
    timeout)

This would be equivalent to

self.host = host
self.port = port
# etc.

I'm not sure if that is technically feasible, but it would cut down on
repetition of names.



More information about the Python-list mailing list