classification
Title: Tutorial and FAQ: how to call a method on an int
Type: behavior Stage: patch review
Components: Documentation, Interpreter Core Versions: Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Jon.Shemitz, cheryl.sabella, docs@python, josh.r, jstasiak, pitrou, r.david.murray, sreepriya, terry.reedy
Priority: normal Keywords: patch

Created on 2014-02-19 22:50 by Jon.Shemitz, last changed 2017-07-03 23:14 by cheryl.sabella.

Files
File name Uploaded Description Edit
doc1.patch sreepriya, 2014-03-11 11:25 review
doc2.patch sreepriya, 2014-03-11 17:14 A new faq : 'Syntax error while calling a method on an integer value' review
doc3.patch sreepriya, 2014-03-12 13:10 A new faq : 'Syntax error while looking up an attribute on an integer literal' review
doc4.patch sreepriya, 2014-03-12 21:42 A new faq : 'Syntax error while looking up an attribute on an integer literal' review
Messages (13)
msg211670 - (view) Author: Jon Shemitz (Jon.Shemitz) Date: 2014-02-19 22:50
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.
msg211674 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-02-19 23:59
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'>
msg211676 - (view) Author: Jon Shemitz (Jon.Shemitz) Date: 2014-02-20 00:04
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>
> _______________________________________
>
msg211861 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-02-21 14:43
Upon consideration, I think you are right: we should add a FAQ and link it from the tutorial.
msg211884 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-02-21 22:53
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).
msg213140 - (view) Author: Sreepriya Chalakkal (sreepriya) * Date: 2014-03-11 11:25
This is a patch that includes the faq.
msg213152 - (view) Author: Sreepriya Chalakkal (sreepriya) * Date: 2014-03-11 17:14
New patch after first review.
msg213276 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-03-12 18:57
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'>
msg213572 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-03-14 17:21
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/
msg213584 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-03-14 18:45
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.
msg213585 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-03-14 19:08
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'>
msg213591 - (view) Author: Sreepriya Chalakkal (sreepriya) * Date: 2014-03-14 21:37
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.
msg297619 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2017-07-03 23:14
Sreepriya Chalakkal,

Would you be able to prepare a pull request on GitHub for your patch?


Thanks!
History
Date User Action Args
2017-07-03 23:14:27cheryl.sabellasetnosy: + cheryl.sabella
messages: + msg297619
2017-07-03 22:55:50jstasiaksetnosy: + jstasiak
2014-12-04 01:52:05josh.rsetnosy: + josh.r
2014-11-26 23:57:11berker.peksaglinkissue22948 superseder
2014-03-14 21:37:22sreepriyasetmessages: + msg213591
2014-03-14 19:08:47terry.reedysettitle: 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
messages: + msg213585
stage: needs patch -> patch review
2014-03-14 18:45:26r.david.murraysetmessages: + msg213584
2014-03-14 17:21:09pitrousetnosy: + pitrou
messages: + msg213572
2014-03-12 21:42:07sreepriyasetfiles: + doc4.patch
2014-03-12 18:57:19terry.reedysetmessages: + msg213276
2014-03-12 13:10:29sreepriyasetfiles: + doc3.patch
2014-03-11 17:14:13sreepriyasetfiles: + doc2.patch

messages: + msg213152
2014-03-11 11:25:31sreepriyasetfiles: + doc1.patch

nosy: + sreepriya
messages: + msg213140

keywords: + patch
2014-02-21 22:53:00terry.reedysetnosy: + terry.reedy
messages: + msg211884
2014-02-21 14:43:55r.david.murraysetstatus: closed -> open
title: Tutorial section 9.4 -> Tutorial section 9.4 and FAQ: how to call a method on an int
messages: + msg211861

resolution: not a bug ->
stage: resolved -> needs patch
2014-02-20 00:04:23Jon.Shemitzsetmessages: + msg211676
2014-02-19 23:59:47r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg211674

resolution: not a bug
stage: resolved
2014-02-19 22:50:24Jon.Shemitzcreate