Guido's new method definition idea

Lie Ryan lie.1296 at gmail.com
Mon Dec 8 02:30:52 EST 2008


On Sun, 07 Dec 2008 12:57:27 +0100, News123 wrote:

> Lie wrote:
>> On Dec 7, 1:02 am, News123 <news... at free.fr> wrote:
>>> What would be interesting would be some syntactical sugar to get rid
>>> of the 'self' (at least in the code body).
>>>
>>> example:
>>> class C:
>>>     class_elements a,b,c,d
>>>
>>>     def method(self,arg):
>>>         global d
>>>         a,b,c = arg[0..3]
>>>         d = a + b
>>>         self.e = a + d
>>>
>>>
>> Nah, that would make it not explicit. Explicit here also means that to
>> refer to self's a, we need to explicitly refer to self.
> 
> Well being explicit when trying to suggest an implicit syntax (in order
> to reduce typing) is a little difficult ;-)
> 
> Though you're right my main goal is not being implicit but would be
> reducing typing and have shorter source code lines.
> 
> If 'global '<varname>' is accepted inside a def, then moving
> 'class_elements <varnames>' inside the def could be acceptable as well
> though it would requiere, that this statement is repeated per def
> 

The advantage of explicit self is to easily differentiate instance 
variable with local variable/names. When I need to shorten the code, I'll 
simply alias it to a local name, no need for syntax change or new keyword.

class C(object):
    def __init__(self):
        self.a = 2
        self.b = 2
        self.c = 3
    def x(self):
        #return ((-self.b + math.sqrt(self.b**2 - 4 * self.a * self.c)) / 
(2 * self.a)), ((-self.b - math.sqrt(self.b**2 - 4 * self.a * self.c)) / 
(2 * self.a))

        a, b, c = self.a, self.b, self.c
        sq = math.sqrt
        return ((-b + sq(b**2 - 4*a*c)) / (2*a)), ((-b - sq(b**2 - 
4*a*c)) / (2*a))




More information about the Python-list mailing list