Idea about method parameters
markus at schabi.de
Wed Sep 26 21:40:40 CEST 2001
Wolfgang Grafen <wolfgang.grafen at marconi.com> schrub:
> Well, Python is flexible to do it anyway and although I missed
> to give an example for that I hoped you could develop by yourself:
>>>> class A:
> ... def __init__(self,a,b,c=333,d=444):
> ... self.__dict__.update(vars())
> ... del self.__dict__['self'] # remove cyclic self reference
> ... def __call__(self):
> ... print self.a, self.b, self.c, self.d
I had such in mind, yes. Your solution is more elegant (especially with
treating the "self"), have my respects.
But it only works if you want to assign _all_ parameters to the
corresponding instance variables. Every other variable (including self)
has to be explicitly treated. In a mixed-case, the "good old" way is
less work and less "obfuscated".
My example in Message <1634093.6GMIcojqen at lunix.schabi.de> shows such a
mixed case (although most of the variables are assigned).
> Answers to your objections (below):
> 1. Now the parameter list is under the programmer's control
> 2. Using self.__dict__ is *not* a dirty hack as already many code
> does use self.__dict__
> 3. Positional parameters are possible as well
> 4. There are default values
> 5. No need for an extra documentation of valid parameters
Fully agree on 1, 3, 4 and 5.
I still consider accessing self.__dict__ somehow hackish - but maybe
that is because of my "static" history. And as it seems that it is
common sense, have my +0.99 :-)
> For me this is good enough and there is no need for a new syntax.
The old way with explicit assignments is also "good enough", as it
works and is well-known.
But for loops are also "good enough" to replace mapping and list
comprehensions, but they still found their way in the language.
One may also create a dictionary using lots of d[key]=value statements,
so there's no need to recycle those curly braces known from C :-)
Don't get me wrong, I love those constructs (Dictionaries, mapping,
list comprehensions and even self.__dict__) - they constitute the
pythonic way of life :-)
But having a "good enough" way should not be a killer argument for a
better possibility. Although your solution is a good argument against
"The strength of the Constitution lies entirely in the determination of
each citizen to defend it. Only if every single citizen feels duty
bound to do his share in this defense are the constitutional rights
secure." -- Albert Einstein
More information about the Python-list