Another try at Python's selfishness

Antoon Pardon apardon at forel.vub.ac.be
Fri Feb 3 07:22:35 EST 2006


Op 2006-02-03, n.estner at gmx.de schreef <n.estner at gmx.de>:
>> First of all, you are using a really poor example of a "method",
>> since it doesn't use any attributes of the Foo instance.
>
> Agreed. I tried to post a short example, and it obviously was to short
> to make my point clear. lets take a longer one. Current syntax:
>
> class Pair:
>     def __init__(self, a,b):
>         self.a = a
>         self.b = b
>
>     def sum(self):
>         return self.a + self.b
>
>     def product (this):
>         return this.a + this.b
>
> My alternative syntax suggestion would be this one:
>
> class Pair:
>     def self.__init__(a,b):
>         self.a = a
>         self.b = b
>
>     def self.sum():
>         return self.a + self.b
>
>     def this.product ():
>         return this.a + this.b
>
>> You are really giving "self" a magic meaning with your suggestion
>> which isn't needed at all.
>
> No. I hope this is clearer in the example above. "self" shouldn't be a
> keyword. It's a special kind of argument now, so why shouldn't we
> explicitly _declare_ that it's a special kind of argument? (as explicit
> is better than implicit)

Self is not a special kind of argument. It is the accessing of the
method that provides for the magic. Simplified one could say the
following is happening.

def _Pair__init__(self, a, b):
    self.a = a
    self.b = b

def _Pair_sum(self):
    return self.a + self.b

def _Pair_product(this):
    return this.a * this.be

class Pair:

    def __init__(self, ...):
        self.__init__ = BoundMethod(self, _Pair__init__)
        self.sum = BoundMethod(self, _Pair_sum)
        self.product = BoundMethod(self, _Pair_product)
        self.__init__(...)


So when p is an instance of Pair, p.sum is not your defined
function but a BoundMethod.

-- 
Antoon Pardon



More information about the Python-list mailing list