[issue20692] Tutorial section 9.4
New submission from Jon Shemitz: The tutorial says "Each value is an object, and therefore has a class (also called its type). It is stored as object.__class__." So, I tried
3.__class__ File "<stdin>", line 1 3.__class__ ^ SyntaxError: invalid syntax
Yet, "foo".__class__ worked, as did 3j.__class__ and 3.5.__class__. When my son (!) suggested that I try (3).__class__, I did indeed get <type 'int'>, while (3,).__class__ gave <type 'tuple'>. This *looks like* a minor error in the parser, where seeing \d+\. puts it in a state where it expects \d+ and it can't handle \w+ This may be the sort of thing that only a newbie would even think to try, so may not be worth fixing. If so, it may be worth mentioning in the tutorial. ---------- assignee: docs@python components: Documentation, Interpreter Core messages: 211670 nosy: Jon.Shemitz, docs@python priority: normal severity: normal status: open title: Tutorial section 9.4 type: behavior versions: Python 2.7 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue20692> _______________________________________
R. David Murray added the comment: It's actually almost a FAQ at this point. The answer is that because of the way the parser works (it's a relatively simple parser, and we want to keep it that way), the tokenizer sees the '.' as making the token a float, and '3.__class__' is not a valid float token. So you have to precede the period by something that allows the tokenizer to know it isn't a decimal point. Parens is one way. Believe it or not, a space is another:
3 .__class__ <class 'int'>
---------- nosy: +r.david.murray resolution: -> invalid stage: -> committed/rejected status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue20692> _______________________________________
Jon Shemitz added the comment: That makes sense. Perhaps, then, the tutorial should include the FAQ? (I can't be the only person who thought to try this.) On Wed, Feb 19, 2014 at 3:59 PM, R. David Murray <report@bugs.python.org>wrote:
R. David Murray added the comment:
It's actually almost a FAQ at this point. The answer is that because of the way the parser works (it's a relatively simple parser, and we want to keep it that way), the tokenizer sees the '.' as making the token a float, and '3.__class__' is not a valid float token. So you have to precede the period by something that allows the tokenizer to know it isn't a decimal point. Parens is one way. Believe it or not, a space is another:
3 .__class__ <class 'int'>
---------- nosy: +r.david.murray resolution: -> invalid stage: -> committed/rejected status: open -> closed
_______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue20692> _______________________________________
---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue20692> _______________________________________
R. David Murray added the comment: Upon consideration, I think you are right: we should add a FAQ and link it from the tutorial. ---------- resolution: invalid -> stage: committed/rejected -> needs patch status: closed -> open title: Tutorial section 9.4 -> Tutorial section 9.4 and FAQ: how to call a method on an int _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue20692> _______________________________________
Terry J. Reedy added the comment: I agree that the tutorial should somewhere make it clear (possibly with a FAQ link) that int literals must be parenthesized or spaced before .name attribute access because <literal>.name is parsed as (<literal>.)name. That is a consequence of float literals not requiring a fractional part (unlike some other languages). ---------- nosy: +terry.reedy _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue20692> _______________________________________
Sreepriya Chalakkal added the comment: This is a patch that includes the faq. ---------- keywords: +patch nosy: +sreepriya Added file: http://bugs.python.org/file34352/doc1.patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue20692> _______________________________________
Sreepriya Chalakkal added the comment: New patch after first review. ---------- Added file: http://bugs.python.org/file34355/doc2.patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue20692> _______________________________________
Changes by Sreepriya Chalakkal <sreepriya1111@gmail.com>: Added file: http://bugs.python.org/file34375/doc3.patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue20692> _______________________________________
Terry J. Reedy added the comment: Replace 'The right way... with ---- To look up an attribute on an integer literal, separate the literal from the period with either a space or parentheses. >>> 3 .__class__ <class 'int'> >>> (5).__class__ <type 'int'> ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue20692> _______________________________________
Changes by Sreepriya Chalakkal <sreepriya1111@gmail.com>: Added file: http://bugs.python.org/file34382/doc4.patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue20692> _______________________________________
Antoine Pitrou added the comment: I am not a native English speaker, but Sreepriya's latest patch looks ok to me (I am not sure the link from classes.rst is useful, though). Sreepriya, have you already signed the contributor's agreement? Otherwise, you can sign it online at http://www.python.org/psf/contrib/contrib-form/ ---------- nosy: +pitrou _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue20692> _______________________________________
R. David Murray added the comment: I might tweak a couple words for flow, but it looks good. I do wonder about the repetition of the bit about parenthesis or whitespace that now exists. I wonder if the first occurrence of it should now be dropped. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue20692> _______________________________________
Terry J. Reedy added the comment: I agree with Antoine about the particular cross-link and would drop that one. Is there somewhere earlier in the tutorial that discusses .attribute access? That would be the place to mention the ints and dotted names. Rather than a link, I would just mention that ints need to be separated from the period. I also agree with David. Here is a condensed answer that I think says just what is needed. --- This is because the Python parser sees an integer literal followed by a period as a float literal and a float literal followed by a name, ``5. __class__``, is a syntax error. To look up an attribute on an integer literal, separate the integer from the period with either a space or parentheses. >>> 5 .__class__ <class 'int'> >>> (5).__class__ <type 'int'> ---------- stage: needs patch -> patch review title: Tutorial section 9.4 and FAQ: how to call a method on an int -> Tutorial and FAQ: how to call a method on an int _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue20692> _______________________________________
Sreepriya Chalakkal added the comment: In tutorials, under section 3.1.1 - Numbers, it is mentioned about the type of integers. And also a statement as "we will see more about numeric types later in the tutorial". May be we can mention about type class there. But it might be too early to mention about classes under Numbers for a learner. Otherwise, I also agree that the cross link is not very essential and could be dropped. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue20692> _______________________________________
Changes by Josh Rosenberg <shadowranger+python@gmail.com>: ---------- nosy: +josh.r _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue20692> _______________________________________
Changes by Jakub Stasiak <jakub+python.org@stasiak.at>: ---------- nosy: +jstasiak _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue20692> _______________________________________
Cheryl Sabella added the comment: Sreepriya Chalakkal, Would you be able to prepare a pull request on GitHub for your patch? Thanks! ---------- nosy: +csabella _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue20692> _______________________________________
Change by Irit Katriel <iritkatriel@yahoo.com>: ---------- keywords: +easy versions: +Python 3.10 -Python 2.7 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue20692> _______________________________________
Amir <amir.rastkhadiv@gmail.com> added the comment: Hi everyone! I'm going to work on it. I have a plan to submit my pull request in the upcoming weeks. Thanks. ---------- components: -Interpreter Core nosy: +Amir.Rastkhadiv20 versions: +Python 3.9 -Python 3.10 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue20692> _______________________________________
participants (10)
-
Amir
-
Antoine Pitrou
-
Cheryl Sabella
-
Irit Katriel
-
Jakub Stasiak
-
Jon Shemitz
-
Josh Rosenberg
-
R. David Murray
-
Sreepriya Chalakkal
-
Terry J. Reedy