explicit self revisited

Peter Maas peter.maas at utilog.de
Mon Nov 13 17:03:55 EST 2006


Steven D'Aprano schrieb:
> Implicit self will never be used for Python, because it is redundant so
> long as there is a need for explicit self, and there is a need for
> explicit self because there are a number of basic, dare I say
> *fundamental* programming techniques that require an explicit self.
> 
> For example, delegation.
> 
> class MyClass(Parent):
>     def method(self, arg):
>         Parent.method(self, arg)
>         # what are you going to write? Parent.method(,arg) maybe?

The idea is: let self (or self.) be represented by the dot alone,
therefore:

Parent.method(., arg) # :)

> Here's another fundamental technique that implicit self breaks.
> 
> class MyList(list):
>     def __iadd__(self, other):
>         # in-place addition
>         other = transform(other) # do something to the argument
>         super(MyList, self).__iadd__(other) # call the superclass
>         # could be written .__class__.__iadd__(other)
>         # BUT be aware the semantics are NOT the same!
>         return self  # and return the suitably modified instance

return .

> You can't explicitly return the instance unless you have an explicit name
> for it. (And if you are thinking of creating more magic syntax that
> implicitly returns self, just don't bother.)

No magic. Just a dot. But perhaps a dot is too tiny. We could take JUST_ME
or ME_AND_BOBBY_MCGEE instead, of course as a reserved keyword followed by a
dot ;)

Thanks for your hints, Steven.

--
Regards/Gruesse,

Peter Maas, Aachen
E-mail 'cGV0ZXIubWFhc0B1dGlsb2cuZGU=\n'.decode('base64')



More information about the Python-list mailing list