Idea about method parameters
Markus Schaber
markus at schabi.de
Fri Sep 28 04:05:15 EDT 2001
Hi,
Wolfgang Grafen <wolfgang.grafen at gmx.de> schrub:
> Attempt 3:
>
>>>> class A:
> ... def __init__(self,a,b,c=333,d=444):
> ... e = d + c
> ... del c,d
> ... self.__dict__.update(vars()) # save a,b,e
> ... del self.__dict__['self'] # remove cyclic self
> reference
> ... def __call__(self):
> ... print self.a,self.b,self.e
> ...
>>>> a=A(1,2)
>>>> a()
> 1 2 777
Accepted :-)
And using the comments, it is explicit enough. Although it gives the
danger that you forget to correct the del when changing the argument
list. But one should know what he does when playing with knifes.
> 2. Accessing self.__dict__ is *not hackish at all*.
> - It is documented in the Python language reference
> - If you issue following command in the python2.1 directory
> grep -il __dict__ *\.py | wc -l
> you will find out that 21 out of 158 modules use this construct!
Okay, it seems that I'll have to accept that - I even catch myself
beginning to use it during the last days :-)
> 6. It also works for a subset of the parameters :)
Okay. Nearly perfect. And I accept it's "good enough" to not justify
messing with the interpreter changes.
> Well, let's finish this discussion here. With the construct above you
> have everything you requested for. Your suggestion to adapt the
> interpreter to also accept "self." as a prefix for the the variables
> in the parameter list
> might give you *your* individual convinience syntax.
My exact suggestion was to accept the first parameter as a prefix for
the others, as you stated yourself some lines below.
> Then there will be a difference between class and
> function parameter list handling.
Okay, this points out a strong technical argument against it. As in
python, functions and methods are the same, this has to be allowed for
all functions. And this gives consequences I never thought about.
(Although everyone can define such a function "by hand" today).
> And forget the beautiful
> small BNF of Python. And end soon with something similar to Perl.
This is also is a technical argument against it, I didn't think about
the formal grammar. This would get much more difficult with my syntax,
I'm afraid.
And I strictly avoided to learn Perl up to now, and I don't want to do
it by "backdoor" makink Python like Perl.
> But now to the *killer argument against your suggestion*:
> Allowing self.<variable_name> in the interfaces describes already an
> implementation detail which heavily violates object orientated coding
> rules.
I don't count this, because the variable_name would be the public
declaration, and the "self." should be thought to be "private", just as
the function body is. So anybody can change the method later by adding
or removing self. of parameters (and changing the inner logic of the
method) without breaking compatibility.
> Not only this, <variable_name> will be the same as
> self.<variable_name>. x=NewClass(self.a=12,self.b=45) would be the
> same as x=NewClass(a=12,b=45)
NewClass(self.a=12, self.b=45) would be illegal (the first parameter
has to be the "self"-Reference).
And having f(self,a=5) be the same as f(self, self.a=5) was what I
wanted.
> Changes for Python take a lot of effort. Write a PEP, discuss your
> proposal
> for at least half a year, persuade at least 50% of the active Python
> community and pray that Guido can accept it. This procedure assures
> Python's
> quality :)
That's the reason for me to start this thread. I wanted to know whether
it is worth this effort.
And I learned (as I always learn here) that there are alternatives that
can accomplish the same functionality and some of them (at least for
experienced programmers) nearly the same readability.
Additionally, your article pointed to bad side-effects on the grammar
and that I didn't think about the behaviour of ordinary functions.
So, should python ever get a preprocessor (I hope it won't), I'll
strongly push my proposal into it, but accept that it is not ready to
go into the main python.
markus
--
"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
mailing list