Attack a sacred Python Cow

Nikolaus Rath Nikolaus at
Mon Jul 28 09:26:02 CEST 2008

castironpi <castironpi at> writes:
>> I think you misunderstood him. What he wants is to write
>> class foo:
>>    def bar(arg):
>>        self.whatever = arg + 1
>> instead of
>> class foo:
>>    def bar(self, arg)
>>        self.whatever = arg + 1
>> so 'self' should *automatically* only be inserted in the function
>> declaration, and *manually* be typed for attributes.
> There's a further advantage:
> class A:
>   def get_auxclass( self, b, c ):
>     class B:
>       def auxmeth( self2, d, e ):
>         #here, ...
>     return B

In auxmeth, self would refer to the B instance. In get_auxclass, it
would refer to the A instance. If you wanted to access the A instance
in auxmeth, you'd have to use

class A:
   def get_auxclass(b, c ):
     a_inst = self
     class B:
       def auxmeth(d, e ):
         self # the B instance
         a_inst # the A instance
     return B

This seems pretty natural to me (innermost scope takes precedence),
and AFAIR this is also how it is done in Java.



 »It is not worth an intelligent man's time to be in the majority.
  By definition, there are already enough people to do that.«
                                                         -J.H. Hardy


More information about the Python-list mailing list