[Tutor] Why is the name "self" optional instead of mandatory?
Alan Gauld
alan.gauld at btinternet.com
Thu Jan 21 04:47:33 EST 2016
On 21/01/16 03:42, boB Stepp wrote:
> So I really only have one question: Why not make Python's
> *traditional* name, "self", mandatory? Why give the programmer this
> kind of choice? [OK, that was two questions.]
Because to do otherwise would introduce all sorts of extra
complexity into the standard function calling mechanism.
There is nothing super special about methods they are just
functions. You can call the parameters of a function anything
you like. To suddenly have the interpreter check the name
of the first positional parameter for the specific case
of a method makes no sense.
And it's very convenient to allow other names. When I'm
playing at the >>> prompt I often just substitute s for self
to save typing. When sharing Python code with C++ or Java
programmers I often use 'this' instead because they will
understand that.
Python gives us flexibility which we are expected to use
responsibly. It's a good thing.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list