[Python-ideas] Mitigating 'self.' Method Pollution

Ron Adam ron3200 at gmail.com
Sat Jul 11 19:16:41 CEST 2015

On 07/11/2015 05:12 AM, Nick Coghlan wrote:
> With Michael's proposal, that changes to:
>      def keep_moving_gravity(self):
>          self y, gravity
>          y += gravity
>          y = max(y, 0)
>          y = min(y, height - 1)

This works now...

    def keep_moving_gravity(self):
        y = self.y + self.gravity
        y = max(y, 0)
        self.y = min(y, height - 1)

I don't think the self comes up as frequently as it may seem in well 
written code.

And in the above, self is written once, but the other names y and gravity 
are repeated again.  So the gain isn't as great as it may seem at first.

What would this do?

    def __init__(self, red, blue, green):
        self red, blue green
        red = red
        blue = blue
        green = green

If the names are long, it would take more characters than the equivalent 
version using self.


More information about the Python-ideas mailing list