[Tutor] understanding classes

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Wed, 14 Aug 2002 14:01:49 -0700 (PDT)


On Wed, 14 Aug 2002, Lance E Sloan wrote:

> --On Wednesday, August 14, 2002 3:07 PM +0100 alan.gauld@bt.com wrote:
> >> I have one question:  As far as you know, is "self" in Python
> >> basically the same as "this" in Java?
> >
> > Exactly so and if it helps you can even call it 'this':
> >
> > class Foo:
> >    baz = "Class Foo"
> >    def bar(this):
> >      return this.baz
>
> Thanks for the confirmation.  Gregor and others confirmed that for me,
> too, also pointing out that it's a keyword in Java, but an explicitly
> defined variable in Python.  Is there a special term to refer to that
> variable, like "instance variable"?
>
> My next question was going to be, why is the Python convention to name
> that variable "self" rather than "this"?  I suspect it's just Guido's
> choice.  Was Java not so well known when this was done?  Or is this an
> intentional choice to distance Python from Java?


There's an extraordinarily important reason for this.  Try:

###
>>> import this
###

at the interactive interpreter.

*grin*