why self instead obj ?

Michele Simionato mis6 at pitt.edu
Fri Mar 7 18:18:37 CET 2003


Brian Elmegaard <brian at rk-speed-rugby.dk> wrote in message news:<uheafd0sy.fsf at mail.afm.dtu.dk>...
> Peter Hansen <peter at engcorp.com> writes:
> 
> > > I am beginner in Python and i wonder why the first argument of  the__init__
> > > function is called "self" instead "obj" whereas self  indicates
> > > the object itself .
> 
> I think a different wording would have made more sense for me, coming
> from a non-objects world. I really have struggled to get used to self,
> but from that viewpoint I don't think obj would be any better. 
> 
> Perhaps the reason for the confusion is that self is used 6 times in
> scripts in the tutorial before it is mentioned in paragraph 5 under
> Random remarks, which I (as an oop and python beginner) didn't even
> try to understand. I don't think the first four paragraphs are
> particularly tutorial-like, so I understand why I never came to the
> self-thing.
> 
> Placing the convention-thing by the first use would make more sense,
> IMHO.
> 
> However, it would also make more pythonic sense to me to call 'self', either
> 'thisObject' or 'thisInstance'. 
> 
> Random remarks:
> It can't be a good enough reason that self is just inherited from smalltalk.
> 
> It is a too easy way out to tell every beginner that they can use
> whatever name for self in their scripts. Usually you start working
> from scripts already existing in the tutorial or demos, and it would
> be quite cumbersome to know that you should start with renaming every
> instance of self with your own favourite term.

You are making two different points here.

1. The tutorial is not a tutorial and it assumes you already know OOP.
2. We could use another name for self.

On point 1 I must agree with you, I also have posted some rant about 
the tutorial.You will have better luck looking at the references in 
http://www.python.org/doc/Newbies.html

On point 2, however, I disagree. Since the choice at the beginning was
arbitrary, I think Guido made a sensible choice with "self", since it
was already used by Smalltalk people. *It is* a reason good enough to me.
Of course, he could also have used "this" as in C++.

It is true that 'self' is long to type, but you can always alias it with
s=self. I must say that at the beginning I was inconfortable with self
myself (if a little pun is admitted ;).

Anyway, when you will start playing with classmethods and metaclasses, 
you will have the satisfaction of renaming 'self' as 'cls' (notice, only
three letters, as 'obj'!) with Guido's benediction ;)


                                        Michele




More information about the Python-list mailing list