Attack a sacred Python Cow
Nikolaus Rath
Nikolaus at rath.org
Mon Jul 28 03:26:02 EDT 2008
castironpi <castironpi at gmail.com> 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.
Best,
-Nikolaus
--
»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
PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C
More information about the Python-list
mailing list