Attack a sacred Python Cow

castironpi castironpi at gmail.com
Sun Jul 27 14:20:38 EDT 2008


On Jul 26, 4:08 am, Nikolaus Rath <Nikol... at rath.org> wrote:
> Terry Reedy <tjre... at udel.edu> writes:
> > Nikolaus Rath wrote:
> >> Terry Reedy <tjre... at udel.edu> writes:
> >>> Torsten Bronger wrote:
> >>>> Hallöchen!
> >>>  > And why does this make the implicit insertion of "self" difficult?
> >>>> I could easily write a preprocessor which does it after all.
> >>> class C():
> >>>   def f():
> >>>     a = 3
>
> >>> Inserting self into the arg list is trivial.  Mindlessly deciding
> >>> correctly whether or not to insert 'self.' before 'a' is impossible
> >>> when 'a' could ambiguously be either an attribute of self or a local
> >>> variable of f.  Or do you and/or Jordan plan to abolish local
> >>> variables for methods?
>
> >> Why do you think that 'self' should be inserted anywhere except in the
> >> arg list? AFAIU, the idea is to remove the need to write 'self' in the
> >> arg list, not to get rid of it entirely.
>
> > Because you must prefix self attributes with 'self.'. If you do not
> > use any attributes of the instance of the class you are making the
> > function an instance method of, then it is not really an instance
> > method and need not and I would say should not be masqueraded as
> > one. If the function is a static method, then it should be labeled
> > as one and no 'self' is not needed and auto insertion would be a
> > mistake. In brief, I assume the OP wants 'self' inserted in the body
> > because inserting it only in the parameter list and never using it
> > in the body is either silly or wrong.
>
> 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.
>
> 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

There's a further advantage:

class A:
  def get_auxclass( self, b, c ):
    class B:
      def auxmeth( self2, d, e ):
        #here, ...
    return B

Because Python permits you to name 'the current instance' any name you
want, you're able to hold more than one current instance at one time.
In this case, you could write 'return self.val+ self2.val' with no
trouble, while I'm not even sure that anything like that is possible
in C, C++, Java, or anything higher other than Python.  (This is a
good point, outside of the usual, by the way.)



More information about the Python-list mailing list