use of "self", was "No Do while/repeat until looping construct in python?"

Alex Martelli aleax at aleax.it
Sat Mar 15 10:53:02 CET 2003


Chermside, Michael wrote:

>> On the other hand, if you have a valid reason why "self" is advantageous
>> FROM THE USER's POINT OF VIEW I would like to hear it.
> 
> I'm not sure I really believe you (about desiring to hear a reason
> why "self" is advantageous), but I'll give it a shot anyhow.
> 
> Consider the following Java snippet (sorry, I don't know Ruby well
> enough to get the syntax right).

Ruby uses @name to mean exactly the same this as self.name means in
Python -- so the Ruby/Python distinction here is first of all one of 
syntax sugar, of *surface* syntax: Ruby's syntax for this is
punctuation-rich and very compact, Python's is wordier and arguably
more readable.  But there's more: Python needs NO black magic, nor
special, ad-hoc language rules to make self.name mean "attribute
`name' of object `self'" -- it's just one case of the general
rule which gives this meaning to all expressions of the overall
form object.attribute.


> variable "jiggerPercent". This could be either a local
> variable OR an instance variabler... and there's no way to

This is a common problem in Java and C++, but not in Ruby,
since instance variables are "stropped" by the leading @ --
so, Ruby is much closer to Python in these terms, even though
people who focus on surface-syntax issues may not be able
to see that.

> Of course, Python isn't perfect in this regard... it still
> has three different namespaces (local, global, built-in) where

If you do think that multiple namespaces are an imperfection,
then you might really like Ruby -- where GLOBAL variables are
also "stropped", in this case by $name.  I've tried it and
didn't like it, but it may be an issue of habit.  The Python
equivalent would be to have a name that is used (by either
rule or convention) to mean "the global namespace", say
glob, and then use glob.name rather than just name to refer
to attributes of that namespace (global variables).  I have
occasionally tried experimenting with that style, by starting
a module with:

glob = sys.modules[__name__]

and I find it has some good points (such as doing away with
the need for the global statement) -- but of course it's more
verbose.  Still, using glob.name and self.name for the global
and instance namespaces, and thus leaving bare name to refer
to a necessarily _local_ variable (...with the obvious
exception of the word 'glob' itself...;-) might be a usable
approach if you were designing a simple language from scratch.


> PS: Problem for the readers: name another advantage of using
> self BESIDES distinguishing local and instance scopes. Extra
> credit if you come up with one I haven't heard before.

What are you comparing it with?  The other term of comparison
determines what the advantages and disadvantages will be, of
course.  E.g., using self.name (Python) vs @name (Ruby) is
equivalent in terms of distinguishing local and instance scopes:
the tradeoffs are rather in terms of compactness/readability,
and need for an extra ad-hoc rule to give meaning to @name vs
not needing any such supplementary rule for self.name.


Alex





More information about the Python-list mailing list