should "self" be changed?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed May 27 01:17:51 EDT 2015


On Wednesday 27 May 2015 02:37, zipher wrote:

> Would it be prudent to rid the long-standing "argument" (pun unintended)
> about self and the ulterior spellings of it, by changing it into a symbol
> rather than a name?

No.


> Something like:
> 
> class MyClass(object):
> 
>     def __init__(@):
>         @.dummy = None
> 
> OR, even better, getting *rid of it* in the parameter list, so it stops
> confusing people about how many parameters a method needs, and transform
> it into a true *operator*.

I think you misspelled a word. There's no "be", "tt" or "er" in "worse" :-)

All joking aside, I cannot imagine why you think that the first argument to 
a method should be considered an operator. It's an argument, a named 
variable, not an operator.


> class MyClass(object):
> 
>     def __init__(): #takes no arguments!

But that is completely wrong. __init__ DOES take a single argument -- the 
instance. Methods, it can be either bound to the instance or unbound:

py> str.upper("hello world")  # unbound method, self provided explicitly
'HELLO WORLD'
py> "hello world".upper()  # bound method, self provided automatically
'HELLO WORLD'

I suggest you study these two examples carefully.



>         @.dummy = None  #the @ invokes the class object's dictionary

It certainly shouldn't do that. It should invoke the full attribute lookup 
machinery, which does far more than invoke the class __dict__. In fact, if 
it invoked the class __dict__, that would *completely* break the object 
oriented paradigm. Writing to the class __dict__ means that all instances 
share the same value.


> That would seem to be a nice solution to the problem, really.  It doesn't
> become PERLish because you've made it into a genuine operator -- "self"
> was always a non-variable that looked like a variable and hence created an
> itch that couldn't be scratched.

That is completely wrong. self is, and always has been, a real variable. 
That's why you can call it anything you like -- self is just the convention, 
nothing more.



-- 
Steven




More information about the Python-list mailing list