PEP: Shorthand Symbol for "self"
Hello, I have written a PEP, and I was warned by none other than Mr. Van Rossum himself that it would be controversial at best. Well, here goes anyway. First, let me introduce myself. I am an aerospace engineer, and I am using Python to develop a research prototype of a conflict alerting aid for air traffic controllers. It is intended to replace the current legacy system which has been operational since the 1970s. I have also developed a free Python package to represent physical scalars in a unique manner that can be as efficient as built-in numeric types. I am using it extensively in my conflict alerting aid. You can read about it in the current edition of The Python Papers at http://pythonpapers.org or on my website at http://RussP.us/scalar.htm . Now to the PEP. Let me start by saying that I fully understand the history and controversy regarding the explicit use of "self" in Python. I am not going to say that it was a mistake, nor am I going to say that the first argument of a class instance method should not refer to the instance for which it is called. What I will say is that I think Python syntax could be significantly simplified with the simple little convention that I am proposing. All I ask is that you carefully read my proposal before you reply. Thank you. Russ
On Sun, Aug 24, 2008 at 3:43 PM, Russ Paielli <russ.paielli@gmail.com> wrote:
Hello,
I have written a PEP, and I was warned by none other than Mr. Van Rossum himself that it would be controversial at best. Well, here goes anyway.
First, let me introduce myself. I am an aerospace engineer, and I am using Python to develop a research prototype of a conflict alerting aid for air traffic controllers. It is intended to replace the current legacy system which has been operational since the 1970s. I have also developed a free Python package to represent physical scalars in a unique manner that can be as efficient as built-in numeric types. I am using it extensively in my conflict alerting aid. You can read about it in the current edition of The Python Papers at http://pythonpapers.org or on my website at http://RussP.us/scalar.htm .
Now to the PEP. Let me start by saying that I fully understand the history and controversy regarding the explicit use of "self" in Python. I am not going to say that it was a mistake, nor am I going to say that the first argument of a class instance method should not refer to the instance for which it is called. What I will say is that I think Python syntax could be significantly simplified with the simple little convention that I am proposing. All I ask is that you carefully read my proposal before you reply. Thank you.
Couple things. One, tilde and carat are not even possible options as they are used for binary negation and XOR, respectively. Two, I disagree that it is in any way difficult to remember what the first argument is since everyone names it either 'self' or 'cls' depending on whether it is a classmethod or not. Three, I don't see how a bunch of dollar signs in code is not just as much clutter as 'self.'. Sure, it's shorter by four characters, but that is not much in the grand scheme of things. Four, I just think it's ugly to have a dollar sign prepend a name like that. And five, the dollar sign is distracting. My eye automatically gravitates to non-letter and non-digit characters, and having the code littered with '$' is going to be distracting, IMO. Thanks for trying, Russ, but this gets a -1 from me. -Brett
On Sun, Aug 24, 2008, Russ Paielli wrote:
Abstract
In Python, the class instance for which a method is called is referred to within the method by the first formal argument. By convention, that first argument is typically named "self". Attributes of "self" are referred to using the standard "dot" notation, as in "self.attr". As a result, complex methods can become cluttered with many occurrences of "self.". This PEP proposes to allow the dollar symbol, "$", to be used as a shorthand symbol for "self." or, in general, for "<arg1>.", where "<arg1>" is the name of the first argument. Thus, "self.attr" could be written more succinctly as "$attr".
<sniff><sniff> Smells like Perl. -1 (Sorry, but that *will* be the automatic reaction from many people, and because your proposal doesn't deal with the issue up-front, it has no chance to fly.) -- Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/ Adopt A Process -- stop killing all your children!
On Sun, Aug 24, 2008 at 6:43 PM, Russ Paielli <russ.paielli@gmail.com> wrote:
... I fully understand the history and controversy regarding the explicit use of "self" in Python.
One of the problems with the current syntax is that calling sites don't match the definition site the way they do for normal functions. This proposal doesn't seem to help with that. Normal function -- def fn(a, b, z=None): ... fn(a, b, z) fn(a, b) Method -- def m(self, a, b, z=None): ... # self is moved outside the parens, changing the tuple size self.m(a, b, z) self.m(a, b) -jJ
Jim Jewett wrote:
On Sun, Aug 24, 2008 at 6:43 PM, Russ Paielli <russ.paielli@gmail.com> wrote:
... I fully understand the history and controversy regarding the explicit use of "self" in Python.
One of the problems with the current syntax is that calling sites don't match the definition site the way they do for normal functions. This proposal doesn't seem to help with that.
Normal function --
def fn(a, b, z=None): ...
fn(a, b, z) fn(a, b)
Method --
Methods are normal functions accessed through a class attribute binding. In 3.0, the unbound method wrapper that might have obscured this is gone.
def m(self, a, b, z=None): ...
# self is moved outside the parens, changing the tuple size self.m(a, b, z) self.m(a, b)
If m is an attribute of type(s) (which is s.__class__), this shrinkage is a convenient abbreviation, not a requirement. The above calls are the same as type(s).m(s.a,b,z) or type(s).m(s,a,b). Or, if you prefer, fn = type(s).m # or s.__class__.m fn(s,a,b,z) If m is an attribute s, and s in not a class, the shrinkage is not available, and one must write s.m(s,a,b,z) or s.m(s.a,b). Terry Jan Reedy
Jim Jewett wrote:
One of the problems with the current syntax is that calling sites
don't match the definition site the way they do for normal functions. This proposal doesn't seem to help with that. ...
def m(self, a, b, z=None): ...
# self is moved outside the parens, changing the tuple size self.m(a, b, z) self.m(a, b)
On Sun, Aug 24, 2008 at 6:26 PM, Terry Reedy <tjreedy@udel.edu> wrote:
If m is an attribute of type(s) (which is s.__class__), this shrinkage is a convenient abbreviation, not a requirement. The above calls are the same as type(s).m(s.a,b,z) or type(s).m(s,a,b). Or, if you prefer, fn = type(s).m # or s.__class__.m fn(s,a,b,z) If m is an attribute s, and s in not a class, the shrinkage is not available, and one must write s.m(s,a,b,z) or s.m(s.a,b).
Terry Jan Reedy
If I could write: class foo: def self.bar(): self.rebar(self.babar) then the call to object.bar() would match the declaration. Back to Russ's proposal: it would be better accomodated IMHO by allowing $ as a character in a variable name, just like _ is. Then, conventionally, people could use $ as self: def $.bar(): $.rebar($.babar) and for whatever it's worth, I find $.bar easier to read then $bar as the explicit dot reminds me it's doing an attribute get rather than looking like a special variable name. --- Bruce
On Mon, Aug 25, 2008 at 3:45 AM, Bruce Leban <bruce@leapyear.org> wrote:
Jim Jewett wrote: On Sun, Aug 24, 2008 at 6:26 PM, Terry Reedy <tjreedy@udel.edu> wrote:
If m is an attribute of type(s) (which is s.__class__), this shrinkage is a convenient abbreviation, not a requirement. The above calls are the same as type(s).m(s.a,b,z) or type(s).m(s,a,b). Or, if you prefer, fn = type(s).m # or s.__class__.m fn(s,a,b,z) If m is an attribute s, and s in not a class, the shrinkage is not available, and one must write s.m(s,a,b,z) or s.m(s.a,b).
Terry Jan Reedy
If I could write:
class foo: def self.bar(): self.rebar(self.babar)
then the call to object.bar() would match the declaration.
+1. I like this proposal: it's clean and sensible, and I think it looks a lot more clear, especially to someone who is new to Python.
Back to Russ's proposal: it would be better accomodated IMHO by allowing $ as a character in a variable name, just like _ is. Then, conventionally, people could use $ as self:
def $.bar(): $.rebar($.babar)
and for whatever it's worth, I find $.bar easier to read then $bar as the explicit dot reminds me it's doing an attribute get rather than looking like a special variable name.
+1 I'm in agreement here as well. The closest I would get to supporting the $-for-self. proposal is allowing $ in variable names. Having used Lisp before Python, I'm used to just about any character being allowed in identifier names. The more characters available, the more expressive the names can be, and I think it could only help things. The overhead of people having to get used to new symbols being used in variable names would be small in comparison to the flexibility offered. Brandon
Brandon Mintern wrote:
Having used Lisp before Python, I'm used to just about any character being allowed in identifier names. The more characters available, the more expressive the names can be, and I think it could only help things.
3.0 uses the standard Unicode syntax for identifiers: " identifier ::= id_start id_continue* id_start ::= <all characters in general categories Lu, Ll, Lt, Lm, Lo, Nl, the underscore, and characters with the Other_ID_Start property> id_continue ::= <all characters in id_start, plus characters in the categories Mn, Mc, Nd, Pc and others with the Other_ID_Continue property> " This is 10s of thousands of characters ;-) -- but no symbols. " The Unicode category codes mentioned above stand for: Lu - uppercase letters Ll - lowercase letters Lt - titlecase letters Lm - modifier letters Lo - other letters Nl - letter numbers Mn - nonspacing marks Mc - spacing combining marks Nd - decimal numbers Pc - connector punctuations All identifiers are converted into the normal form NFC while parsing; comparison of identifiers is based on NFC. " I think we should stick with this international cross-language standard. tjr
On Mon, Aug 25, 2008 at 11:46 AM, Terry Reedy > 3.0 uses the standard Unicode syntax for identifiers:
" identifier ::= id_start id_continue*
id_start ::= <all characters in general categories Lu, Ll, Lt, Lm, Lo, Nl, the underscore, and characters with the Other_ID_Start property>
Are you sure? IIRC, the unicode consortion themselves recommend the xid_start and xid_continue properties instead. (One of the quirks of unicode is that it will gladly shoot itself in the foot to support backwards compatibility, and this is one of those cases. Characters that really shouldn't be used for identifiers got mixed in by mistake. There are even more restrictive proposals for security, but those were deemed excessive.) -jJ
Jim Jewett schrieb:
On Mon, Aug 25, 2008 at 11:46 AM, Terry Reedy > 3.0 uses the standard Unicode syntax for identifiers:
" identifier ::= id_start id_continue*
id_start ::= <all characters in general categories Lu, Ll, Lt, Lm, Lo, Nl, the underscore, and characters with the Other_ID_Start property>
Are you sure?
IIRC, the unicode consortion themselves recommend the xid_start and xid_continue properties instead.
I thought we were already using them. Georg
On Wed, Aug 27, 2008 at 6:25 PM, Georg Brandl <g.brandl@gmx.net> wrote:
Jim Jewett schrieb:
On Mon, Aug 25, 2008 at 11:46 AM, Terry Reedy > 3.0 uses the standard Unicode syntax for identifiers:
" identifier ::= id_start id_continue*
id_start ::= <all characters in general categories Lu, Ll, Lt, Lm, Lo, Nl, the underscore, and characters with the Other_ID_Start property>
Are you sure?
IIRC, the unicode consortion themselves recommend the xid_start and xid_continue properties instead.
I thought we were already using them.
PEP 3131 says we should. Terry's email said we don't. I can't find the section of code that would tell me directly. -jJ
Brandon Mintern wrote:
The more characters available, the more expressive the names can be,
Also the more confusing they can be. It's bearable in Lisp because there are very few characters that have special meaning to the parser, and most symbols are set off by whitespace or parentheses. But in Python, most punctuation is syntactically special, and names are set off by operators at least as often as whitespace. So allowing just a few randomly-chosen punctuation chars as part of names would be confusing, I think. I find that Ruby code with ! and ? in variable names trips up my visual parser, for example. I see something like set! and it looks like the name 'set' followed by a '!' operator. -- Greg
Just for fun, I made a copy of one of my Python files with hundreds of occurrences of "self", and I replaced them all with "S". This streamlines things significantly, and I think it looks good, but I suppose most Python aficionados would be aghast. I'm thinking about doing it permanently for all my files. I normally avoid single-character names, but I think this case could be a reasonable exception. What would you think if you saw this in "production" code? --Russ On Mon, Aug 25, 2008 at 3:35 AM, Brandon Mintern <bmintern@gmail.com> wrote:
On Mon, Aug 25, 2008 at 3:45 AM, Bruce Leban <bruce@leapyear.org> wrote:
Jim Jewett wrote: On Sun, Aug 24, 2008 at 6:26 PM, Terry Reedy <tjreedy@udel.edu> wrote:
If m is an attribute of type(s) (which is s.__class__), this shrinkage
is
a convenient abbreviation, not a requirement. The above calls are the same as type(s).m(s.a,b,z) or type(s).m(s,a,b). Or, if you prefer, fn = type(s).m # or s.__class__.m fn(s,a,b,z) If m is an attribute s, and s in not a class, the shrinkage is not available, and one must write s.m(s,a,b,z) or s.m(s.a,b).
Terry Jan Reedy
If I could write:
class foo: def self.bar(): self.rebar(self.babar)
then the call to object.bar() would match the declaration.
+1. I like this proposal: it's clean and sensible, and I think it looks a lot more clear, especially to someone who is new to Python.
Back to Russ's proposal: it would be better accomodated IMHO by allowing $ as a character in a variable name, just like _ is. Then, conventionally, people could use $ as self:
def $.bar(): $.rebar($.babar)
and for whatever it's worth, I find $.bar easier to read then $bar as the explicit dot reminds me it's doing an attribute get rather than looking like a special variable name.
+1 I'm in agreement here as well. The closest I would get to supporting the $-for-self. proposal is allowing $ in variable names. Having used Lisp before Python, I'm used to just about any character being allowed in identifier names. The more characters available, the more expressive the names can be, and I think it could only help things. The overhead of people having to get used to new symbols being used in variable names would be small in comparison to the flexibility offered.
Brandon _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
On Mon, Aug 25, 2008 at 06:41:06PM -0700, Russ Paielli wrote:
Just for fun, I made a copy of one of my Python files with hundreds of occurrences of "self", and I replaced them all with "S". This streamlines things significantly, and I think it looks good, but I suppose most Python aficionados would be aghast. I'm thinking about doing it permanently for all my files. I normally avoid single-character names, but I think this case could be a reasonable exception. What would you think if you saw this in "production" code?
I'd be against it because it violates a well-established convention. I consider having a One True Style (a naming convention in this particular case) is a Good Thing. I am accustomed to that naming convention; if I see code with a different convention I'd have problems reading and understanding it. Oleg. -- Oleg Broytmann http://phd.pp.ru/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.
Hallöchen! Russ Paielli writes:
Just for fun, I made a copy of one of my Python files with hundreds of occurrences of "self", and I replaced them all with "S". This streamlines things significantly, and I think it looks good, but I suppose most Python aficionados would be aghast. I'm thinking about doing it permanently for all my files. I normally avoid single-character names, but I think this case could be a reasonable exception. What would you think if you saw this in "production" code?
The "self" convention goes so far that my editor highlights it -- this may be true for other editors/HTML highlighters, too. I'd even frown upon any programmer who breaks this convention for code that has the slightest chance to become public. Besides, I don't consider "self"s cluttering up the code. YMMV, but I don't measure code legibility by the terseness of certain identifies at all. Tschö, Torsten. -- Torsten Bronger, aquisgrana, europa vetus Jabber ID: torsten.bronger@jabber.rwth-aachen.de
My editor, Xemacs, also highlights the word "self." I personally find that a bit annoying, and perhaps that is part of the reason I don't appreciate the word. When I write an algorithm, I don't want to see some artifact highlighted, as if it is somehow the most important part of the algorithm. I like seeing keywords highlighted, but I don't want to see "self" highlighted because it is only a convention. Maybe I'm alone on that. As I wrote earlier, I copied one of my larger Python files and replaced each occurrence of "self" with "S". That was approximately 350 occurrences. I also replaced each occurrence of "cls" with "C", which was another 50 or so occurrences. The file size was reduced by 2.8%, many wrapped lines no longer need to wrap, and I like the way it looks. I understand that people like to have "one true way," but I think Python programmers are adaptable enough to handle this one. When you read code, you certainly need to read the argument names anyway, so the fact that "S" is being used instead of "self" should be obvious. The only minor disadvantage I can think of is that searching on a single character can be problematic, but if you are using an IDE, that should not be an issue (and I can't remember ever searching on "self" anyway). One of these days, when I get a chance, I may write an informational PEP recommending the use of "S" as an acceptable replacement for "self", and "C" as an accptable replacement for "cls". I hope that doesn't blow any fuses. Have a nice day. --Russ On Mon, Aug 25, 2008 at 11:58 PM, Torsten Bronger < bronger@physik.rwth-aachen.de> wrote:
Hallöchen!
Russ Paielli writes:
Just for fun, I made a copy of one of my Python files with hundreds of occurrences of "self", and I replaced them all with "S". This streamlines things significantly, and I think it looks good, but I suppose most Python aficionados would be aghast. I'm thinking about doing it permanently for all my files. I normally avoid single-character names, but I think this case could be a reasonable exception. What would you think if you saw this in "production" code?
The "self" convention goes so far that my editor highlights it -- this may be true for other editors/HTML highlighters, too. I'd even frown upon any programmer who breaks this convention for code that has the slightest chance to become public.
Besides, I don't consider "self"s cluttering up the code. YMMV, but I don't measure code legibility by the terseness of certain identifies at all.
Tschö, Torsten.
-- Torsten Bronger, aquisgrana, europa vetus Jabber ID: torsten.bronger@jabber.rwth-aachen.de
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
On 27 aug 2008 at 02:02:39, Russ Paielli <russ.paielli@gmail.com> wrote:
My editor, Xemacs, also highlights the word "self." I personally find that a bit annoying, and perhaps that is part of the reason I don't appreciate the word. When I write an algorithm, I don't want to see some artifact highlighted, as if it is somehow the most important part of the algorithm. I like seeing keywords highlighted, but I don't want to see "self" highlighted because it is only a convention. Maybe I'm alone on that.
As I wrote earlier, I copied one of my larger Python files and replaced each occurrence of "self" with "S". That was approximately 350 occurrences. I also replaced each occurrence of "cls" with "C", which was another 50 or so occurrences. The file size was reduced by 2.8%, many wrapped lines no longer need to wrap, and I like the way it looks.
I understand that people like to have "one true way," but I think Python programmers are adaptable enough to handle this one. When you read code, you certainly need to read the argument names anyway, so the fact that "S" is being used instead of "self" should be obvious. The only minor disadvantage I can think of is that searching on a single character can be problematic, but if you are using an IDE, that should not be an issue (and I can't remember ever searching on "self" anyway).
One of these days, when I get a chance, I may write an informational PEP recommending the use of "S" as an acceptable replacement for "self", and "C" as an accptable replacement for "cls". I hope that doesn't blow any fuses.
Have a nice day.
--Russ
I absolutely disagree. It's the same thing that happens when we use one character identifiers: the code becomes less readable. Identifiers must be self-explicative. Cheers, Cesare
Normally I agree with you on the single-character identifiers, but I consider this case an exception. Lets not forget that C++ and Java use zero-character identifiers for "self", and I don't hear a din of complaints that it harms readability. My code does not appear to me to be any less readable to me when I substitute "S" for "self". The fact that it is a capital letter helps a bit here, I think. The fact that it usually appears as "S.attr" rather than just "S" also makes it stand out clearly. I just don't see the problem with having an "approved" shorthand form for self when it does not require any changes in the language itself. --Russ On Wed, Aug 27, 2008 at 12:11 AM, Cesare Di Mauro <cesare.dimauro@a-tono.com
wrote:
On 27 aug 2008 at 02:02:39, Russ Paielli <russ.paielli@gmail.com> wrote:
My editor, Xemacs, also highlights the word "self." I personally find that a bit annoying, and perhaps that is part of the reason I don't appreciate the word. When I write an algorithm, I don't want to see some artifact highlighted, as if it is somehow the most important part of the algorithm. I like seeing keywords highlighted, but I don't want to see "self" highlighted because it is only a convention. Maybe I'm alone on that.
As I wrote earlier, I copied one of my larger Python files and replaced each occurrence of "self" with "S". That was approximately 350 occurrences. I also replaced each occurrence of "cls" with "C", which was another 50 or so occurrences. The file size was reduced by 2.8%, many wrapped lines no longer need to wrap, and I like the way it looks.
I understand that people like to have "one true way," but I think Python programmers are adaptable enough to handle this one. When you read code, you certainly need to read the argument names anyway, so the fact that "S" is being used instead of "self" should be obvious. The only minor disadvantage I can think of is that searching on a single character can be problematic, but if you are using an IDE, that should not be an issue (and I can't remember ever searching on "self" anyway).
One of these days, when I get a chance, I may write an informational PEP recommending the use of "S" as an acceptable replacement for "self", and "C" as an accptable replacement for "cls". I hope that doesn't blow any fuses.
Have a nice day.
--Russ
I absolutely disagree. It's the same thing that happens when we use one character identifiers: the code becomes less readable.
Identifiers must be self-explicative.
Cheers, Cesare
Russ Paielli wrote:
Normally I agree with you on the single-character identifiers, but I consider this case an exception. Lets not forget that C++ and Java use zero-character identifiers for "self", and I don't hear a din of complaints that it harms readability.
(Not) from who? I think in general Pythonistas complain that the "zero-character identifiers" harm readability. Someone already said this in this thread: Andy Toulouse wrote:
I don't like programming in languages that don't make as clear a distinction between local variables and instance variables.
In the rare cases that I am writing Java or C++ these days, I absolutely use "this" explicitly despite the lack of a requirement to do so. It's simply more readable in the long-term.
My code does not appear to me to be any less readable to me when I substitute "S" for "self". The fact that it is a capital letter helps a bit here, I think. The fact that it usually appears as "S.attr" rather than just "S" also makes it stand out clearly.
I just don't see the problem with having an "approved" shorthand form for self when it does not require any changes in the language itself.
Feel free to write code like this for yourself. I don't see anyone "approving" any sort of "shorthand form" ever. The only official stance on coding conventions is intended to guide the writing of code for the stdlib. I find it very unlikely anyone would accept "S" in place of "self" within the stdlib. Furthermore, I think the likelihood of having another developer working with your code will sharply go down if you stray from the guidelines for the stdlib. But to paraphrase what someone else in this thread already said: what you do in private is your business. -Scott P.S. Top-posting is inappropriate for this list and makes it more difficult to follow long discussions (like this one). -- Scott Dial scott@scottdial.com scodial@cs.indiana.edu
Russ Paielli schrieb:
Normally I agree with you on the single-character identifiers, but I consider this case an exception. Lets not forget that C++ and Java use zero-character identifiers for "self", and I don't hear a din of complaints that it harms readability.
Yes, because most have coding styles that mandate hungarian prefixes on such identifiers. And "mInfo" isn't really more readable than "self.info". Georg
Russ Paielli wrote:
Lets not forget that C++ and Java use zero-character identifiers for "self", and I don't hear a din of complaints that it harms readability.
I'm not so sure about that. Frequently people adopt conventions such as always writing this->x, or naming all instance variables with an "m_" prefix, suggesting that zero characters is considered a problem by many. -- Greg
On Wed, Aug 27, 2008 at 8:38 PM, Greg Ewing <greg.ewing@canterbury.ac.nz>wrote: Russ Paielli wrote:
Lets not forget that C++ and Java use zero-character identifiers for "self", and I don't hear a din of complaints that it harms readability.
I'm not so sure about that. Frequently people adopt conventions such as always writing this->x, or naming all instance variables with an "m_" prefix, suggesting that zero characters is considered a problem by many.
And that's despite the fact that they have more assistance from IDEs for autocomplete, highlighting, etc. George
Russ Paielli wrote:
My editor, Xemacs, also highlights the word "self." I personally find that a bit annoying, and perhaps that is part of the reason I don't appreciate the word. When I write an algorithm, I don't want to see some artifact highlighted, as if it is somehow the most important part of the algorithm. I like seeing keywords highlighted, but I don't want to see "self" highlighted because it is only a convention. Maybe I'm alone on that.
Change your editor such that the highlighting de-emphasizes the word self (by making it closer to the background color or whatever; this is what I do myself), and you should be all set then. -Jacob
On Tue, Aug 26, 2008 at 5:02 PM, Russ Paielli <russ.paielli@gmail.com> wrote:
My editor, Xemacs, also highlights the word "self." I personally find that a bit annoying, and perhaps that is part of the reason I don't appreciate the word. When I write an algorithm, I don't want to see some artifact highlighted, as if it is somehow the most important part of the algorithm. I like seeing keywords highlighted, but I don't want to see "self" highlighted because it is only a convention. Maybe I'm alone on that.
As I wrote earlier, I copied one of my larger Python files and replaced each occurrence of "self" with "S". That was approximately 350 occurrences. I also replaced each occurrence of "cls" with "C", which was another 50 or so occurrences. The file size was reduced by 2.8%, many wrapped lines no longer need to wrap, and I like the way it looks.
I understand that people like to have "one true way," but I think Python programmers are adaptable enough to handle this one. When you read code, you certainly need to read the argument names anyway, so the fact that "S" is being used instead of "self" should be obvious. The only minor disadvantage I can think of is that searching on a single character can be problematic, but if you are using an IDE, that should not be an issue (and I can't remember ever searching on "self" anyway).
One of these days, when I get a chance, I may write an informational PEP recommending the use of "S" as an acceptable replacement for "self", and "C" as an accptable replacement for "cls". I hope that doesn't blow any fuses.
You can submit anything you want. You know what Guido is going to say with regards to self -> S and cls -> C as a proposed change to the PEP 8 style guide? Something along the lines of "no". He may even be that polite. What you do in your own code (that you write and maintain) is your own business. If S/C makes you happy: use it. But don't be surprised if/when someone starts working on that same code they start asking (perfectly valid) questions about why you chose to go against the PEP 8 style guide. - Josiah
On Mon, Aug 25, 2008 at 12:45 AM, Bruce Leban <bruce@leapyear.org> wrote:
Jim Jewett wrote:
If I could write:
class foo: def self.bar(): self.rebar(self.babar)
then the call to object.bar() would match the declaration.
Back to Russ's proposal: it would be better accomodated IMHO by allowing $ as a character in a variable name, just like _ is. Then, conventionally, people could use $ as self:
def $.bar(): $.rebar($.babar)
and for whatever it's worth, I find $.bar easier to read then $bar as the explicit dot reminds me it's doing an attribute get rather than looking like a special variable name.
def $.bar(): $.rebar($.babar) That's two separate proposals, but I think I like both of them. Of course, Python already allows "S", which is very similar to "$", as the first argument, so we're almost there on that aspect of it. Come to think of it, I may start using "S" and see how it works out. So how about def S.bar(): S.rebar(S.babar) --Russ
I really don't think any of the proposed solutions are logically, functionally or aesthetically better than what we currently have with "def foo(self)". I find all proposed variants of "$.foo" to be jarring. "def self.bar()" is somewhat less ugly, but misleading since the method is actually being bound to its outer class, not "self" (presumably, any instances of that class rather than the class itself):
class Foo(object): ... def bar(self): ... print "Hello, 0x%x" % id(self) ... foo = Foo() Foo.bar(foo) Hello, 0xb7729f8c foo.bar() Hello, 0xb7729f8c
Note that the declaration of "bar" binds the function to class "Foo", not to the instance "foo". Would these semantics change with the "self.bar()" syntax? Either way it's a -1 from me until a more Pythonic proposal surfaces. I'm not sure there's a way to improve on the simplicity of the existing semantics though. Cheers, T Russ Paielli wrote:
On Mon, Aug 25, 2008 at 12:45 AM, Bruce Leban <bruce@leapyear.org <mailto:bruce@leapyear.org>> wrote:
Jim Jewett wrote:
If I could write:
class foo: def self.bar(): self.rebar(self.babar)
then the call to object.bar() would match the declaration.
Back to Russ's proposal: it would be better accomodated IMHO by allowing $ as a character in a variable name, just like _ is. Then, conventionally, people could use $ as self:
def $.bar(): $.rebar($.babar)
and for whatever it's worth, I find $.bar easier to read then $bar as the explicit dot reminds me it's doing an attribute get rather than looking like a special variable name.
def $.bar(): $.rebar($.babar)
That's two separate proposals, but I think I like both of them.
Of course, Python already allows "S", which is very similar to "$", as the first argument, so we're almost there on that aspect of it. Come to think of it, I may start using "S" and see how it works out.
So how about
def S.bar(): S.rebar(S.babar)
--Russ
------------------------------------------------------------------------
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
Bruce Leban wrote:
If I could write:
class foo: def self.bar(): self.rebar(self.babar)
then the call to object.bar() would match the declaration.
I would rather reserve that syntax for a possible future enhancement to allow the target of a def statement to be a more general lvalue. It's been suggested e.g. that it would be handy to be able to create tables of functions using things like def funcs[5](x, y): ... If that were allowed, then def a.b() would logically have the meaning of defining a function b as an attribute of a. -- Greg
Russ Paielli wrote:
Another option is to explicitly "strip off" the "self." by writing "attr = self.attr" to create a local reference to the attribute within the method.
This is very often done for another reason -- it's *much* faster to refer to a local variable than to look up an attribute, so if the attribute is being used more than once or twice, it's more efficient to pull it into a local first. Given that, methods which use attributes of self a lot tend not to look as cluttered as you might expect. -- Greg
Russ Paielli wrote: ...
instance for which it is called. What I will say is that I think Python syntax could be significantly simplified with the simple little convention that I am proposing. All I ask is that you carefully read my proposal before you reply. Thank you.
I have. It strikes me as an improvement over .attr and other ideas posted on c.l.p. In Python, the class instance for which a method is called is referred to within the method by the first formal argument. Somehow awkward. By convention, that first argument is typically named "self". For public code among English speakers. But how far this extends and for how long, given Unicode identifiers in 3.0, is a moot point for your proposal. In Python, class instance methods are functions in which the first formal argument refers to the instance for which the method was called. In Python, class instance methods are functions accessed as an attribute of the class, in particular of the object passed as the first argument. To me your discussion is weakened by the implication that Python has a method class separate from functions. (Bound methods are specialized partial-function wrappers; functools.partial is the general version.) For instance could even work for regular functions if the first argument is an object with attributes and/or methods that can be accessed with standard "dot" notation. Def statements produce functions. Period. This proposal takes no position of whether the latter should be allowed or not. To repeat: former *is* latter with a particular access path. So the question is not whether to 'allow' $ to work in functions; that *is* your proposal, whether you know it or not. To do otherwise would require some other significant change to Python, one that would complicate it. Four possible candidates for the choice of that symbol are "~", "@", "^", and "$". You left out "?" (not too good ;-) and now, in 3.0, "`" (backtick, no longer an repr synonym). The latter is lighter than "$" but perhaps more visible than ".", depending on the font. Hmmm, a comparison: self.d = sqrt(self.x*self.x + self.y*self.y) s.d = sqrt(s.x*s.x + s.x*s.x) .d = sqrt(.x*.x + .y*.y) $d = sqrt($x*$x + $y*$y) `d = sqrt(`s*`x + `y*`y) I think I like this last best. Terry Jan Reedy
On Sun, Aug 24, 2008 at 8:22 PM, Terry Reedy <tjreedy@udel.edu> wrote:
Russ Paielli wrote: ...
instance for which it is called. What I will say is that I think Python syntax could be significantly simplified with the simple little convention that I am proposing. All I ask is that you carefully read my proposal before you reply. Thank you.
I have. It strikes me as an improvement over .attr and other ideas posted on c.l.p.
In Python, the class instance for which a method is called is referred to within the method by the first formal argument.
Somehow awkward.
By convention, that first argument is typically named "self".
For public code among English speakers. But how far this extends and for how long, given Unicode identifiers in 3.0, is a moot point for your proposal.
In Python, class instance methods are functions in which the first formal argument refers to the instance for which the method was called.
In Python, class instance methods are functions accessed as an attribute of the class, in particular of the object passed as the first argument. To me your discussion is weakened by the implication that Python has a method class separate from functions. (Bound methods are specialized partial-function wrappers; functools.partial is the general version.) For instance
could even work for regular functions if the first argument is an object with attributes and/or methods that can be accessed with standard "dot" notation.
Def statements produce functions. Period.
This proposal takes no position of whether the latter should be allowed or not.
To repeat: former *is* latter with a particular access path. So the question is not whether to 'allow' $ to work in functions; that *is* your proposal, whether you know it or not. To do otherwise would require some other significant change to Python, one that would complicate it.
Four possible candidates for the choice of that symbol are "~", "@", "^", and "$".
You left out "?" (not too good ;-) and now, in 3.0, "`" (backtick, no longer an repr synonym). The latter is lighter than "$" but perhaps more visible than ".", depending on the font. Hmmm, a comparison:
Minor point, but the backtick is NOT up for grabs, at least as of a discussion on this very list from last year. Quoting a response from Guido: """ On 1/2/07, Chris Rebert <cvrebert@gmail.com> wrote:
Thus, I propose one of the following as the new use for the backtick (`):
You're missing one of the main reasons for removing the backtick syntax in the first place: the character itself causes trouble by looking too much like a regular quote (depending on your font), is routinely mangled by typesetting software (as every Python book author can testify), and requires a four-finger chord on Swiss keyboards. No new uses for it will be accepted in Python 3000 no matter how good the idea. -- --Guido van Rossum (home page: http://www.python.org/~guido/) """ Not that I understand the relevance of Swiss keyboards, but anyway, there you have it. Perhaps we need to alias __future__.backtick to __future__.braces so people remember this ;) - Chris ======== Follow the path of the Iguana... Rebertia: http://rebertia.com Blog: http://blog.rebertia.com
self.d = sqrt(self.x*self.x + self.y*self.y)
s.d = sqrt(s.x*s.x + s.x*s.x)
.d = sqrt(.x*.x + .y*.y)
$d = sqrt($x*$x + $y*$y)
`d = sqrt(`s*`x + `y*`y)
I think I like this last best.
Terry Jan Reedy
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
participants (18)
-
Aahz
-
Brandon Mintern
-
Brett Cannon
-
Bruce Leban
-
Cesare Di Mauro
-
Chris Rebert
-
Georg Brandl
-
George Sakkis
-
Greg Ewing
-
Jacob Rus
-
Jim Jewett
-
Josiah Carlson
-
Oleg Broytmann
-
Russ Paielli
-
Scott Dial
-
Terry Reedy
-
Thomas Lee
-
Torsten Bronger