Idea about method parameters

Markus Schaber markus at schabi.de
Tue Sep 25 05:10:17 EDT 2001


Greg Ewing <greg at cosc.canterbury.ac.nz> schrub:

> Markus Schaber wrote:
>> 
>> This is why I gave the following semantic definition:
>> 
>> class A:
>>     def m(self, value):
>>         self.value = value
>>         del value
>>         #the other work here
> 
> You may have to pin this down a bit more. How literally are
> we to take the idea that value is bound and then del'ed?
> For example, what should this do:
> 
>   class A:
>     def m(self, self.x, x):
>       print x
> 
> ?

This would raise an SyntaxError, just as
    def m(self, x, x):
currently raises such an error. For the caller, the Parameter should 
just look as it would be an ordinary parameter called x. See also my 
"preprocessor-alike" description I gave in the Message with the 
Message-ID <5243253.PRQshPE9Mp at lunix.schabi.de>.

I have two reasons for this:

Imagine you have

  class A:
    def m(self, count, self.x=0, self.y=0):
      #do something with count here

Now you can call it just like:
  o.m(y=5, count=3)
So you don't give up the ability to give parameters by name.

And when your algorithm gets more complex, you can change your method 
later, without breaking compatibility for users of your class:

  class A:
    def m(self, count, x=0, self.y=0):
      if x<0:
        self.mirrored = 1
        self.x = -x
      else:
        self.mirrored = 0
        self.x = x
      #do something with count here

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